Hello, I’m totally new to test automation in postman.
What I wanted to do:
I call POST endpoint which returns only session-id in JSON format:
{
“session-id”: “234wer234wer”
}
In the Tests section, I wanted to save this session_id as an environment variable to use it as a header in next endpoints:
var jsonData = JSON.parse(responseBody);
pm.environment.set("SessionId", jsonData.session-id);
SessionId - is the name of the environment variable which I added earlier.
But after calling the endpoint, code in Test section fails with this error:
ReferenceError: id is not defined
And I’m not sure how to change it to get the result I want.
hello @danny-dainton,
I have one more question - I moved to next requests, and I tried to use similar structure in Test sections for a different request:
var jsonData = JSON.parse(responseBody);
pm.environment.set("eventId", jsonData["event_id"]);
but currently, instead of event_id in variable value I’m getting null, any idea why? This is the response of the request:
The event_id property is inside an object{} which is inside the eventsarray[].
You need to add event to the path and then reference the position of the object within the array, in your case, it’s the first object so that would be [0] as the list is zero-indexed.
let jsonData = pm.response.json();
pm.environment.set("eventId", jsonData.events[0]["event_id"]);
If you had more than 1 object and you wanted to store each event_id as a variable, you can loop through the array:
Hi! For the latter example here (having more than one object and storing each event_id as a variable) – how do you then run/loop those variables in other request as a parameter?