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
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.
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.
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.
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.
console.log(jsonData[0].gystServerResponse);
console.log(jsonData[0].gystServerResponse[0]);
console.log(jsonData.gystServerResponse[0]);
0 = first element.
Same here @michaelderekjones 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.
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.
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.
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).
Not like this (which is still a string).
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.
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!