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
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}}
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));
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With