Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: How to use environment variables in Request Body

I am trying to replace the value of JSON Body in the Postman from environment variable, and the value is actually replaced but it converts the JSON into a string when passed to the POST request so the request eventually fails since the POST request expects JSON body..

For example, Here is the below request and the variables are already declared with values in the Postman

Screenshot of Postman Body

And once the request is sent, the entire body is converted to String. Any idea how to keep the body is JSON even after substituting the value for the variable and not let it convert to String?

enter image description here

like image 449
Sam Avatar asked May 08 '18 19:05

Sam


People also ask

How do you pass dynamic values in the Postman body?

Let's say you have to pass an integer number from 1 to 1000, so for that, you need to add {{$randomInt}}. Like the above example of the number variable, Postman supports so many other dynamic variables as well.

Can we import environment variables in Postman?

You can access your environment variables from Postman and from your request elements, including the URL, parameters, body data, and test scripts.

How can we apply environment in Postman?

To set up Postman environment variables: In the top right corner of Postman, click the environment selector and select Manage environments. Click Add to add a new environment where you'll define your OneLogin environment variables. Note: You'll need to use your API credentials to generate the access_token value.


1 Answers

Example, I have environment variable : variable name = port, initial value = 4242, current value = 4242. variable name = name, initial value = abcd, current value = abcd. To use environment variable as request body, use this in request body:

{
 "string": "{{name}}",
 "number": {{port}}
}
like image 161
Mudzia Hutama Avatar answered Oct 25 '22 09:10

Mudzia Hutama