JSON Stringify request body using raw

Hello,

We’re using API Gateway and the request body needs to be stringified before sending. Right now, I the request body with a json under “raw” and the content-type of application/json.

I’ve tried using:

var body = {
name: 'Test'
};

pm.environment.set('req_body', JSON.stringify(body));

Then passing in {{req_body}}, but it’s coming in as the whole stringified object as a key with an empty value.

Any ideas in this particular scenario how to get this to work in Postman?

Thank you

HI @davems

Could you give a bit more details please? I tried the following which seemed to work as expected

Pre-request script
image

Body
image

Console after sending (pretty)
image
or raw
image
(Note: Google replied with Error 405 (Method Not Allowed)!! as you’d expect.)

For future user’s landing on this issue:

Make sure your request body is only this (no quotes around the variable name, otherwise it’ll get converted to a string):

{{req_body}}

and not this:

"{{req_body}}"

Here’s a collection showing you how to do it directly in Postman, feel free to fork it and edit it to your needs: Postman

1 Like

thank you so much for your answer.

1 Like

Hi, I have some problem regarding getting timeCardId on my respose body. It’s Getting Null value.
Please Help



What if I only want one field to use this JSON, not the entire request body? e.g.
{
“SomeProperty”: “SomeValue”,
“JsonProperty”: {{jsonValue}}
}
How should I set the {{jsonValue}}?

1 Like