17

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

Sam
  • 375
  • 1
  • 2
  • 13
  • 4
    Isn't the issue simply that you're not specifying valid JSON? You need to quote the dates in your template. – Oliver Charlesworth May 08 '18 at 19:10
  • 1
    @OliverCharlesworth - And I need to import those variables from a JSON file. But when I import it from a JSON file, then the body is still is sent as a request. Any idea how to get the value from JSON file instead of the environment variable and use it in the Body? – Sam May 08 '18 at 20:03
  • Everything stored in an environment file is a string - Do you need a specific value to not be a string? Sorry I’m confused with what you’re trying to ask. – Danny Dainton May 08 '18 at 21:47
  • Can you confirm what headers you’re using in the request - hard to tell from the images. – Danny Dainton May 09 '18 at 22:53
  • 3
    Does this answer your question? [How to pass variables from JSON to postman body](https://stackoverflow.com/questions/48164223/how-to-pass-variables-from-json-to-postman-body) – klemen kobau Dec 13 '19 at 08:12

1 Answers1

16

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}}
}
Mudzia Hutama
  • 414
  • 3
  • 8