Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a postman request body in one place and pass when run

How to set the request body in one place and reuse it in multiple requests so I save effort maintaining test scripts if the build changes.

I am using postman for test automation in a dynamically changing environment. the json body structure might change from build to another, I have to update each request separately.

Here is a sample body where i pass the values from global setter

{        "phone": "{{phone}}",
         "income": {{income}} 
}

these variables are defined in the prerequest as

pm.globals.set("phone", "xxxxxxxx953");
pm.globals.set("income",10);

TIA

like image 349
Nada Nasser Avatar asked Apr 03 '18 13:04

Nada Nasser


1 Answers

By what you say, I understand you want to have a mutable json body structure defined only in one place and then reuse it by specifying different values on different requests. You can achieve this, by using the following:

Include the value of a variable in the Body > raw tab from each request you want to configure this way, for instance:

{{rawBody}}

enter image description here

Define the JSON object to be sent in the folder (or even collection) Pre-request Script:

var obj = {
        phone: "{{phone}}",
        income: "{{income}}"
    };
pm.environment.set("rawBody", JSON.stringify(obj));

enter image description here

Finally, on the request Pre-request Script tab specify the values corresponding to the request:

pm.environment.set("phone", "xxxxxxxx953");
pm.environment.set("income", 10);

enter image description here

By this, you can handle many requests and modify their json body text at once. Obviously, if you want to specify different values for each request you will have to specify them on the request Pre-request Script tab.

like image 128
Lluís Suñol Avatar answered Sep 19 '22 00:09

Lluís Suñol