Set response value as Variable in Test

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:

Screenshot 2023-04-11 at 12.53.28 PM

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.
Screenshot 2023-04-11 at 12.56.18 PM

Any idea what I’m doing wrong?

You’re using 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 first object’s id value:

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:

Thank you for the help! This worked exactly the way I’d expected!

1 Like