Skip to content

Ho to transform and wrap a json received from api gateway method and send to SQS?

0

I receive a json from a method

{
  "nice": "to have",
  "really", true
}

I need to send it to an SQS queue. Policies already are ok, my problem is how to write VTL to send this as message body

{
   "sourceType": "kind-1",
   "rawBody":  {
          "nice": "to have",
          "really", true
    }
}

this works

Action=SendMessage&MessageBody=$input.body

But it doesn't wrap my data

I cannot use string concatenation.

I cannot user JSON.stringify .. damn...

I obtain only transformation errors, without any explanations

ChatGpt, Claude and Perplexity are all giving me wrong suggestions

1 Answer
0

Use a small VTL map, then JSON-encode + URL-encode it for SQS:

#set($wrapped = $util.parseJson("{}")) #set($wrapped.sourceType = "kind-1") #set($wrapped.rawBody = $util.parseJson($input.body))

Action=SendMessage&MessageBody=$util.urlEncode($util.toJson($wrapped))

Notes: Content-Type must be application/x-www-form-urlencoded. For FIFO queues add: &MessageGroupId=... (and &MessageDeduplicationId=... if not content-based).

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.