Getting a specific variable from a list of variables

Basics:
I have never coded in postman and I may not refer to the correct nomenclature or naming. If so, I am sorry, but I am happy to learn :slight_smile:

Scope:
I am calling a server to get various values from the server.
it may be login tokens, IDs, carts etc.
I also have several postman environments with a copy of each environment but with a change in the server domain name only. I would like to understand how I can simplify this mess of environments.

Issue:
This is an example of a script I have an issue with. This is applicable to other server response calls also.

Question:
1.How can I now pick the token from the response from the server? OR any of the other varibles?
2. how should I get this to work? Please share example code as I have read through some of the postman areas, but I do not have enough coding knowledge to understand it.

Code:
This is a a call to get a login token to an ecommerce site

URL: {{base_url}}/rest/V1/integration/customer/token?username=Paste_username&password=Paste_password

Test environment tab:

pm.collectionVariables.set("customer_token","token");

Response in postman:

[
    {
        "token": "<token in the response>",
        "status": 200,
        "success": true,
        "message": "allowed"
    }
]

Hey @pfrenn :wave:

Welcome to the Postman Community! :postman:

You would first need to get all the response using:

const resp = pm.response.json();

From here you will be able to get specific parts of the data that you have saved to the local variable.

As your response is an array and you want to access the first object in the array, you need to use [0] if you didnโ€™t use that, the stored value would be undefined.

const resp = pm.response.json();
pm.collectionVariables.set("customer_token", resp[0].token);

Will this stored as a Collection variable, you can use this in the URL by using the {{customer_token}} variable name.


This are all quite basic workflows in Postman and I would highly recommend working through the training in these Collections, to get more familiar with these fundamental techniques.

https://www.postman.com/postman/workspace/postman-galaxy-training

Thank you for swift reply.
Keen to see if it worked I pasted your code into the call and the token is not allocated to the โ€œcustomer_tokenโ€ value.

A bit puzzled since the customer_token is in the environment.

That was setting a Collection level variable in the script, I know thatโ€™s not the Collection Variables section as you cannot change the type of those.

If you open your Collection in a new tab, there is a Variables sections within that.


This is all covered in those training course, to avoid answering a series of similar questions, I would highly suggest you work through those courses or at the very least, take a look at our documentation.

Thank you.

I am using the postman app in windows and it does not have that tab.

I will look into those courses.

Every OS version has that tab. :thinking:

In the right sidebar, click on your Collection. That will open the Collection in a tab like it does when you open a request from the Collection.

Alternatively, in the script change collectionVariables for environment and you would see it listed in the view you have.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.