Dynamic body with dynamic variables

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”};

1 Like

Hi Alex,

That’s a strange one! The core parts of your script are all working for me in the way that you’d expect - here’s a stripped-down version which always uses prodBody to set the req_body variable:

Snippet
const moment = require('moment');

pm.environment.set('today', moment().format("YYYY-MM-DD"));

pm.environment.set('externalId', '{{$randomInt}}');

var prodBody = '{"origin":"PC","type":"WRITTEN","user":{{externalId}}, "date": "{{today}}", "type2": "unique"}';

pm.collectionVariables.set('req_body',prodBody);

When I post that payload to https://postman-echo.com/post then the console (and the mirrored response) reveal that the variables were substituted as you’d expect:

If this example works for you too, then there are a couple of things I’d suggest checking:

  • Is your pm.environment.name variable being set as you’d expect (have you confirmed you’re executing the code path that you’re intending to?)
  • Is your qaBody variable setup in the same way as prodBody? You’d mentioned experimenting with different formats; the one in prodBody is working for me, so if you’re seeing the same with my example then maybe try double-checking what qaBody is doing.
1 Like

Body

Thanks for the response
I copied and pasted exactly the same code, when I open the collection variables I can see that the variable is not being substituted

Response code is 200, but data is still empty

It’s correct that the collection variable doesn’t show the substituted values (mine is the same). The collection variable contains the literal value that you asked it to store; when the body is submitted, the collection variable is substituted into the body, and that’s when the dynamic variables are replaced.

It’s weird that your screenshotted response has a completely empty data value - this indicates that it’s substituting {{req_body}} with a blank value. Out of interest, do you have a req_body variable set up in your environment variables? Because of the way that variable scopes operate, if you had a variable with the same name in your environment variables then this would take precedence over the collection variable set by your script.

3 Likes

Hey it worked thanks, the problem was basically duplicated variable inside collection and environment at the same time with same name.

This is the code that worked for me

if(pm.environment.name == ‘Prod’ || pm.environment.name == ‘Staging’)

{

pm.collectionVariables.set('body', prodBody);

}

else if(pm.environment.name == ‘QA’){

pm.collectionVariables.set('body', qaBody);

}

console.log(pm.collectionVariables.get(“body”));

Then in the body:

{{body}}

1 Like