title: Intercept and modify SQS SendMessageRequest
body: <p>I'm using <code>Java AWS SDK v1</code> to send messages to a SQS Queue, and I want to intercept this event to add some <code>MessageAttributes</code> in the message.</p>
  <p>I created a new RequestHandler class that extend the <a href="https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/handlers/RequestHandler2.html" rel="nofollow noreferrer">RequestHandler2</a> from the SDK to modify it before the request:</p>
  <pre><code>class CustomMessagesAttributesRequestHandler : RequestHandler2() {
  
      override fun beforeRequest(request: Request&lt;*&gt;) {
          val originalRequest = request.originalRequestObject
  
          if (originalRequest is SendMessageRequest) {
              originalRequest.messageAttributes[&quot;my-custom-attribute&quot;] =
                  MessageAttributeValue().withStringValue(&quot;123456789&quot;)
          }
      }
  }
  </code></pre>
  <p>The solution above kinda works, it's called <code>before</code> the request, the Message Attribute is added in the <code>originalRequestObject</code>, but when the SQS client send the request it throws an exception:</p>
  <blockquote>
  <p>Unable to calculate the MD5 hash of the message attributes</p>
  </blockquote>
  <p>Digging into AWS SDK code, I see there is a default handler to check the message request with the result to compare both body and attributes MD5, and of course, since I modified the original object they does not match.</p>
  <p>Is there a better way to achieve that besides the custom RequestHandler? Or there is a way to recalculate the originalRequestObject MD5?</p>