Using a previous Postman requests JSON response data for the following Postman request

I have set up a Collection variable in the Test tab of my first Postman request as follows:

const jsonData = pm.response.json();
pm.collectionVariables.set(‘gSession’, jsonData.gSession);

I hardcode the body for the first Postman request as follows…

"{
"gSession": "1",
"gEnd": "1",
"gReq": "21",…etc
}

This much works fine. The server responds with a good JSON body.

I want to use the gSession collection variable in the JSON body of the next Postman request as follows…

"{
"gSession": “{{gSession}},
"gEnd": "1",
"gReq": "1",…etc
}”

But this does not work - the server gives an error.

Any help much appreciated.

@teamgyst,
please have a look in Postman console to check what is actually sent in your request (the JSON document).

The {{gSession}} variable should be substituted with the actual value.

Also, check if the JSON is well-formed, i.e. does not contain any redundant/missing quotes, commas, etc.

Best regards,
Karel

It works for me
Here is request1

Here is request2 with gSession in the body

1 Like

Many thanks for the quick reply @KarelHusa and @TestQA2019 .

My mistake on the original post. I am actually trying to send a string representing a JSON object to the server, so it looks like this:

"{
"gSession": "1",
"gEnd": "1",
"gReq": "21",…etc

I’m sure the escape chars are interfering with the collection variable representation, though I’m not sure how to use that variable in my particular use case.

Also, I’m relatively new to Postman so I’m not sure how to view the Console window you have in your screen shot @TestQA2019. I’m using POST method and can only see the POST request and response panels/windows.

Thanks again for your kind assistance.

Hi @teamgyst

The console log can be found in the bottom left corner;

image

IDK how I missed that. Many thanks @w4dd325

Just checking back here…any ideas on how I can use the method @TestQA2019 describes above with a string as follows…

"{
“gSession”: “1”,
“gEnd”: “1”,
“gReq”: “21”,…etc
}
[/quote]

Hi @teamgyst
The example shown (gSession) was a string.
Could you be a little more specific with what you mean?

Hi @w4dd325 and thanks for the reply.

That’s correct, the example I posted has leading and ending quotes, so it is a string. But the example @TestQA2019 posted above does not, as it appears to be JSON. So I wanted to know if the variable is supposed to work either way as it did not seem to work for me with a string.

could you share a screenshot of what you have tried and the console output?

Ohh I just re-read it, you mean that the JSON is in double quotes.
I couldn’t see the closing double quote in your example.

My first question would be; why is your JSON being passed as a string?


If it is definitely being passed as a string, then you would probably need to use a regEx and Slice, like this;

const response = pm.response.text();
let gSession = response.match(/gSession\": \"(.*?)\"\,/gm);
console.log(gSession[0].slice(12, -2));

Hi @w4dd325 and thanks for the code above. I’ll try it out.

The server this goes to requires a string. It receives data via http GET method. I’m curious, does that seem unusual or non-standard to you?

Thanks

I’m not a JSON expert or anything but It just seemed a little odd that the body would be formatted using JSON but then passed as a string.

Maybe someone with more API/Dev skills could educate me if my thought here is incorrect.

I was able to get the server to accept JSON and tried it as @TestQA2019 indicated above.

It works fine now.

Again, many thanks @w4dd325 and @TestQA2019