Using raw response in next request

Hi - Iā€™m trying to use the current API response value, which is a raw number from 1 - 1000 in my subsequent API request. Iā€™ve tried setting up a Global variable and assigning the response to that but it never seems to get picked up in the next request. Screen shot attached here. Any help much appreciated.

The response data isnā€™t JSON so itā€™s erroring (the red dot you can see in the test tab) because youā€™re trying to parse it as that.

Youā€™ll need to use pm.response.text() and also the variable name should be a string value (in quotes) and not like you have it.

Many thanks @danny-dainton - that fixed that.

Quick follow up if I mightā€¦Iā€™m trying to use

pm.collectionVariables.set(ā€˜NextSessionā€™, LastSession);
and/or
pm.globals.set(ā€œNextSessionā€, LastSession);

in my Pre-request and Test Scripts to assign LastSession (local scope variable ) to NextSession (global) - but it never seems to hold that value between requests in the same collection. I read somewhere that .set was not supported in Scripts at one pointā€¦do you know if thatā€™s still the case?
Thanks again

I canā€™t think of a time that itā€™s not been supported :thinking:

You wouldnā€™t be able to use it on something like iterationData in a script but the rest should be fine.

I donā€™t really know the full context of what youā€™re doing or how you have it set up so itā€™s going to be difficult to know whatā€™s happening for you.

Maybe try adding some visual examples of the requests.

Apologies @danny-dainton - I should have done that on the first post


. Screens attached here.

@teamgyst

Code in the ā€œPre-request scriptā€ tab run before the request, and code in the ā€œTestsā€ tab runs afterwards.

The code on line 1 of your pre-request script needs to have a variable to ā€˜getā€™ the results into.
Therefore the message is correct, GystSession is not currently defined.

Although if you are calling a global variable, this isnā€™t needed. Its a global variable, and you can just call it direct when you need to from any request within Postman collection. However, you do seem to be mixing Collection variables with Global variables as well as having another variable with the same name in the Tests. I canā€™t see where you actual set a Global variable. I suspect it should be a Collection variable as that is what youā€™ve set in your Tests tab. Same principle though, you can call a Collection variable from any request in the collection so you donā€™t need to define it in your pre-request script.

Give the variables a different name or pre-fix, so you 100% know which is what. collGystSession, envGystSession, globalGystSession, etc.

Call the variable on line 2 of your Tests something else, like ā€œresponseā€ to differentiate it.

Thanks @michaelderekjones

I removed the globals and renamed the collection variables.

My first API call works fine now (GystStart - screen 1)

But my second one (GystNode1 - screens 2 & 3) still canā€™t find the collection variable named PmGystSession. It seems like Iā€™m missing something in the get/set process for collection variables still. Apologies, Iā€™m still relatively new to Postman. Any help much appreciated.



@teamgyst

Letā€™ start with your first screen shot.

You want to see the value of the variable, not the jsonData.

pm.collectionVariables.set("testVariable", "Test Variable value");
console.log(pm.collectionVariables.get("testVariable"));

If you are want to access a variable from within a request body, wrap its reference in double-quotes:

{ "customer_id" : "{{testVariable}}" }

You canā€™t do this.

console.log("{{testVariable}}"));

From your example, the variable should be set to ā€œ2ā€?

Your second screenshot appears to be showing 1, which looks incorrect but that might just be when you created the screenshots.

The code in your third screenshot is not really doing anything. Itā€™s not needed. Itā€™s just reading from the value. The pre-request is not doing anything. Itā€™s just reading existing data. Itā€™s not setting anything.

The reason you are getting an error though is because you need to store the results into a variable.

var variableName = pm.collectionVariables.get("PmGystSession");
console.log(variableName)

Or more directly.

console.log(pm.collectionVariables.get("PmGystSession"));

But as this is not setting anything, it just seems superfluous.

Thanks @michaelderekjones

To your question aboveā€¦From your example, the variable should be set to ā€œ2ā€?

The answer is yes. What Iā€™m trying to do is assign this value of 2, which is returned by the server in the GystStart API call (the first call to the server), to GystSession in the GystNode API call (the second call to the server). Do you know how I can do this?

Thanks

Can you write the variable to the console log, as it should already be 2.

Can you try again and post the screen shots please (including the console logs) for the first two requests. (Ignore the 3rd one for now)

Sureā€¦I am sending just the first one here for now as I think this is where the problem isā€¦my assignment of the server response to testVariable2.

Iā€™m not sure Iā€™m using Postman correctly to assign jsonData.gystServerResponse in the screen shot.

@teamgyst

Try writing it to the console log in the format you want before trying to set variables. I always do this for troubleshooting purposes. Start at the highest level, and then refine your parameters until you get the node\leaf you want, although in your case, its a single attribute. For more complex JSON, this is the approach I would take.

console.log(jsonData);
console.log(jsonData.gystServerResponse);

This way you will see when its undefined, and needs to be rectified.

jsonData will be a JavaScript object.

I suspect it might need to be one of these.
Which one Iā€™m not sure as this is where my JavaScript falls short and I end up trying them all until one works. :slight_smile:

console.log(jsonData[0].gystServerResponse);
console.log(jsonData[0].gystServerResponse[0]);
console.log(jsonData.gystServerResponse[0]);

0 = first element.

Same here @michaelderekjones :grinning: Iā€™m more of a c/c++/c# developer

I tried them all and, of course, get everything except the one I needā€¦screen attached.

Any other ideas I can try?

Thanks again for your kind assistance here.

@teamgyst

Can I see the original response body in raw format.
On closer inspection I donā€™t think its JSON, but a string (its wrapped in quotes).

You could also write tests for this.

pm.test(ā€˜jsonData is whatā€™, () => {
pm.expect(jsonData).to.be.an(ā€˜stringā€™);
});

Change string to object, and see which one passes. I suspect its a string.

@teamgyst

Have a look at the following.

var stringTest = "{\"gystServerResponse\":100}";
console.log(stringTest);

pm.test('stringTest is an string', () => {
    pm.expect(stringTest).to.be.an('string');
});

var jsonData = JSON.parse(stringTest);
console.log(jsonData);

pm.test('jsonData is an object', () => {
    pm.expect(jsonData).to.be.an('object');
});

console.log(jsonData.gystServerResponse);

Console logs.

image

Looking back at your latest screenshot, your first line is parsing the response (this is the same as JSON.parse) so it should be an object at this point, but for some reason looks to be a string.

So lets take it back to the beginning and just clear out the code you have and just put.

const jsonData = pm.response.json();
console.log(jsonData);

pm.test('jsonData is an object', () => {
    pm.expect(jsonData).to.be.an('object');
});


It should look like this in the console. (an object).

image

Not like this (which is still a string).

image

That worked for me too!

Then I replaced your hardcoded assignment of stringTest with the server response and it too worked.

So now, I think I just need to replace stringTest with a collection variable like gystSession so I can use that in the GystNode1 Params like {{gystSession}}. Do you know how Iā€™d do that?

Thanks @michaelderekjones this is very helpful.

@teamgyst

You can now set your collection variable as you were doing earlier.

pm.collectionVariables.set('pmGystServerResponse', jsonData.gystServerResponse);
console.log(pm.collectionVariables.get('pmGystServerResponse')); // should return 100

But what I donā€™t understand and hopefully someone can chip in here, is why it has to be parsed twice to get it into an object. pm.response.json() and JSON.parse are the same thing. JSON.parse is part of the Javascript implementation, and pm.response.json() is Postmans implementation.

What happens if you try the following?

var jsonData = JSON.parse(pm.response);

Does that return a string or an object.

Edit: Can you also try this?

var jsonData = JSON.parse(responseBody)

@danny-dainton I saw a thread on Stackoverflow in relation to the two methods that you commented on. Do you have any insight here?

Actually reading back through the entire thread, the very first response from @danny-dainton explains that the response to one of the other API calls wasnā€™t JSON, but text and I suspect its the same for this API. It looks like JSON, but its not.

This would make more sense although Iā€™m not sure why itā€™s not erroring on the first line.
Sounds like it should be pm.response.text() and you can then use JSON.parse to turn it into an object.

The code works fine now, as expected, with lines 1,2,7 and 8 per the screenshot.

With line 4 added it gives a JSON Error
With Line 5 added it returns undefined.

Many thanks @michaelderekjones and @danny-dainton for the awesome support!

1 Like