Home > Software engineering >  How to add JSON from "Body" in the query parameter?
How to add JSON from "Body" in the query parameter?

Time:04-03

I'm trying to send a message to an SQS queue. I have everything setup correctly. I'm using a fifo queue, so my post string looks like this:

https://queuename?Action=SendMessage&MessageBody=TEST&MessageGroupId=6&MessageDeduplicationId=6

The above works and the body of the message is TEST, However, I'd like to send data in JSON format In the body tab, I have my payload formatted in JSON. How do I get that JSON value into the MessageBody field as a variable?

CodePudding user response:

Step 1. Save json in a variable

const body = {
    "key": "value"
}

//encoded the special character to make it valid in URL
const payload = encodeURIComponent(JSON.stringify(body))

//Put it in an environment variable
pm.environment.set("payload", payload)

Step 2: Use this var in URL

https://queuename?Action=SendMessage&MessageBody={{payload}}&MessageGroupId=6&MessageDeduplicationId=6
  • Related