11

I want to parameterized my tests in Postman. This is the example body of POST request:

{
  "entity_id": "{{entity_id}}",
  "text": data.comment_name
}

entity_id is global variable and it works correctly, but I want to set value of text from JSON file. data.comment_name doesn't work it saved me exactly data.comment_name as text variable. I want to include this JSON file with all variables in collection runner. How can I do that?

3
  • Have you tried this without adding the “data” part and using just {{comment_name}} Jan 9, 2018 at 8:51
  • that's work, thank you :)
    – robmax
    Jan 9, 2018 at 9:09
  • Awesome! I've added an answer to the question. If it's correct and works for you, please close out this question. Jan 9, 2018 at 9:25

2 Answers 2

19

If your data file looks something like this JSON example:

[
    {
        "entity_id": 1,
        "comment_name": "This is my comment_name"
    }
]

To reference the values in the file, the POST body needs to look like this:

Postman Body

This is how your example would be but you don't need to add the " " around the variable. This could cause issues if the value is a number and adding the quotes around the value in the request body, would make this a string and possibly lead to a bad request.

-1

Pre-request Script Add variable to JSON body

By using pre-request scripts we can change the variable values, parameters, headers, and body data before a request runs by execting JavaScript.

Post URL : http://postman-echo.com/post Code tested in Postman-win64-8.12.4-Setup

Request Body - raw

{
    "locale": "en_US",
    "key"   : "{{dynamic_key}}",
    "value" : "{{dynamic_val}}"
}

Writing pre-request scripts

pm.collectionVariables.set("dynamic_key", "key1");
pm.collectionVariables.set("dynamic_val", "val1");

console.log('Add variable to JSON body', pm.request.body);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.