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 @dannydainton - 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 @dannydainton - 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 @mdjones

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 @mdjones

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 @mdjones :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 @mdjones 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)

@dannydainton 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 @dannydainton 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 @mdjones and @dannydainton for the awesome support!

1 Like