I am trying to figure out how to set a response value as a variable using a Test script so I can pass that value along to another call. All the tutorials I’ve seen do this AFTER setting the value in a Pre-request script, and the logic provided for that doesn’t seem to if you’re not setting the value yourself.
The response I get when I make the API call is as follows:
I’d like to set the “id” value, which is “164070” as a variable called folderID that gets passed along to the next call.
Right now I have the following Test code written, but it’s returning the entire response body as the folderID value.
Any idea what I’m doing wrong?
pm.response.text() here which isn’t required for a JSON response, as you can use
.json() to get the data in a format that’s easily parsable.
This is what you would need to get the
let response = pm.response.json();
Ensure that you have an
environment file created and selected or it will not capture the variable.
I wrote this a while ago about getting different values from a JSON response, it may help you here:
Extracting values from a JSON response
I notice lots of questions from the community which all have a similar flavour - Users are having trouble extracting data from a JSON response body when the value is within an array.
For these examples I’ll be using the
https://randomuser.me API, this returns lots of random user data, in a JSON form…
Thank you for the help! This worked exactly the way I’d expected!