Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.
Here’s an outline with best practices for making your inquiry.
My question:
Hello, I need to send a dynamic body depending on the environment where I’m working, but since the amount of things that will change from one environment to another one is huge, I have decided to make the entire body a variable and inside include some dynamic values that I need. I have removed most of them since it is production data but in the example, you can see the idea.
I have a externalId which is also dynamic, I got it from another request, and then dynamically I need to add it inside this post, the same with today’s value. Unfortunately with this format, I’m not seeing the value external Id and date to be dynamic inside the JSON, it is not being substituted.
I have been trying to use this solution Change request body (RAW JSON) dynamicalls - #13 by zmes50416
Thanks in advance
Details (like screenshots):
This is the code
const moment = require(‘moment’);
pm.environment.set(“today”, moment().format(“YYYY-MM-DD”));
console.info("today date : " + pm.environment.get(“today”));
var prodBody = ‘{“origin”:“PC”,“type”:“WRITTEN”,“user”:{{externalId}}, “date”: {{today}}, “type2”: “unique”}’;
const prodEnv = [‘Prod’, ‘Staging’];
if(prodEnv.includes(pm.environment.name) )
{
pm.collectionVariables.set('req_body',prodBody);
}
else{
pm.collectionVariables.set('req_body', JSON.stringify(qaBody));
}
console.log(pm.collectionVariables.get(‘req_body’));
This is the value that I have in the body
{{req_body}}
This is what I get from the console when I execute the request, as you can see the variables are not being updated successfully. I know that those values are correct
How I found the problem:
Trying to create dynamic body with dynamic variables inside
I’ve already tried:
I have done the following cases, also removing quotation but neither of them have worked
pm.collectionVariables.set(‘req_body’,prodBody);
pm.collectionVariables.set(‘req_body’,JSON.stringify(qaBody));
var prodBody = {“origin”:“PC”,“type”:“WRITTEN”,“user”:{{externalId}}, “date”: {{today}}, “type2”: “unique”};