4

I am getting a signature in WebHook in a particualar header. I would like to pass the same to SQS either in Token Body or Attribute. I have tried following configuration. It is not working. Please help

Following are the details:

AWS Service : Simple Queue Service (SQS) 
HTTP method :POST
Path override :Account/QueueName?Action=SendMessage
Execution role :arn:aws:iam::Acc#:role
Content Handling :Passthrough

Body Mapping Templates :
Content-Type : application/json

{
   "MessageBody" : {
       "payload": $input.json('$'),
       "x-api-key" : "$input.params('x-api-key')"
   }
}

Instead of MessageBody, I also tried with "body". 
Response I get :
{
   "Error": {
      "Code": "MissingParameter",
      "Message": "The request must contain the parameter MessageBody.",
      "Type": "Sender"
},
 "RequestId": "d5fc3acf-18dc-5379-9af2-6b4cc42358f3"
}

What am I missing? Please help. I have spent almost whole day trying to figure it out.

Thanks in Advance.

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
Rohit Kumar
  • 101
  • 1
  • 7
  • I think the inputs to sqs are wrong. Here is an example https://medium.com/@gayanhewa/api-gateway-and-service-proxy-with-sqs-2699c6960690, and here's another answer http://stackoverflow.com/questions/41097441/how-to-integrate-api-gateway-with-sqs – Abhigna Nagaraja May 14 '17 at 21:10
  • I tried the solution given on https://medium.com/@gayanhewa/api-gateway-and-service-proxy-with-sqs-2699c6960690. Unfortunately it didn't work for me. In the second example, they are just adding the body (which I am able to pass). I want to do extra edit to body and add a header to it or pass the header in attribute. – Rohit Kumar May 15 '17 at 14:22

2 Answers2

4

I faced a similar issue recently,

I did the following changes in the integration request:

  1. I removed everything from query parameters
  2. Added Action and MessageBody in mapping templates
  3. Request body passthrough set to When no template matches the request Content- Type header
  4. Content Handling set to Passthrough

Below is the code snippet I added in Mapping Template:

Action=SendMessage&MessageBody={
  "data" : $input.json('$'),
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end
    
    #end  
  }
}

Below is the screenshot for reference: enter image description here

This worked for me and it started pushing headers along with body in the SQS.

Below is a sample message which is getting pushed in the queue:

{ "data" : {"body-key1":"body-value-1"}, "headers": { "header1": "value1"
} }

So what's happening here is when I am sending a post request on queue URL without any query parameter it takes the body directly from the mapping template.

Hope this answer could help anyone.

0

The SQS SendMessage API requires that the MessageBody parameter be passed as a query string parameter. You can configure API Gateway to pass this parameter through from any input parameter, but unfortunately there is currently no way to manipulate the MessageBody using basic parameter mapping to add additional parameters to the message body. If the SQS API took the MessageBody parameter as the POST body on an http request, then you could do it with a mapping template, but unfortunately, it requires the MessageBody to be passed as a query string parameter.

The only way to accomplish this currently is to use a Lambda function. You can use a mapping template to pass both the original MessageBody and the api key to your Lambda function and then combine them in your Lambda function and call the SQS SendMessage API with the combined MessageBody in a query string parameter.

MikeD at AWS
  • 3,565
  • 16
  • 15