AWS API Gateway as Proxy for SQS - Data arrives without positive sign
Asked Answered
M

1

7

I created an API gateway that receives JSON data and posts it to SQS queue. My problem is: if I post data with a POSITIVE SIGNAL (+), for example, {"date": "25 + 2"} the positive signal is replaced by a space, the data arrives in the SQS queue like this: {"date": "25 2"}.

Although, if I send this same message {"date": "25+2"} through the AWS console, the message is received unchanged, with a plus sign.

Why?

Do I need to add additional configuration?

My API Gateway Configuration:

enter image description here

My Post:

curl --location --request POST 'https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/yyyy' \
--header 'Content-Type: application/json' \
--data-raw '{"data": "25+2"}'

Data received in the SQS queue:

enter image description here

P.S: When I get this message through an application like .NET C#, the data also arrives without the positive signal.

Mourant answered 7/9, 2021 at 19:51 Comment(0)
M
10

After the help of a friend and carefully reading the AWS documentation, I found the solution.

The AWS SQS queue expects to receive the message in www-form-urlencoded format and not a JSON (or XML) format.

Therefore, we have two solutions for the case of the question:

  1. When calling the API already send the encoded message.

  2. Or... configure the template in API Gateway to convert the message received in the body to the application/x-www-form-urlencoded format, using the $util.urlEncode function, as shown below:

Mapping template:

Action=SendMessage&MessageBody=$util.urlEncode($input.body)
Mourant answered 12/10, 2021 at 13:22 Comment(3)
This was just what I was looking for, however, APIGateway seems to be dropping the MessageBody now that I'm using the util function.. still looking.Macao
I moved the MessageBody key to before the MessageGroupId key and it work. Couldn't be last for some reason.Macao
@TristanPerotti did you ever end up solving this? Moved message body to before `ActionMicrogroove

© 2022 - 2024 — McMap. All rights reserved.