[Postman] The variables

[Postman] The variables
From advanced to expert
For developers and testers.
———
Some great tutorials to get all the tips of Postman :

Number 2 : Let’s continue the tutorials with the variables.
In the previous tutorial we written some javascript in the test section to get data from the console. Postman offer a storage feature for this data.

That means :

we can use getter and setter inside postman
we can reuse data for a request chain for e2e tests and automation
We can secure bearer token as secret using the variables

For this tutorial we will use the free API of national french repository of adresses (–> documentation – FR)

Create a new request and type this call :

https://api-adresse.data.gouv.fr/search/?q=8+rue+port

Feel free to enter the adresse of your choice 😉

Click the send button to submit the request and enjoy the 5 results.

Alright, it’s time to play with variables !

Firstly we will iterate on the results to get each adresse name :

let adresses = pm.response.json()
adresses.features.forEach(adresse => console.log(adresse.properties.label))

Submit the call and enjoy the results in the dev console :

Each adresse label is properly retrieved.
Secondly, we will store the results in a collection variable.

let adresses = pm.response.json()
// Setter
pm.collectionVariables.set(“adresse_label_variable”, adresses.features[0].properties.label);

Super nice ! And we add a getter :

// Getter
let firstAdresse = pm.collectionVariables.get(“adresse_label_variable”);
console.log(‘stored_variable’, firstAdresse);

Should be like this :

Well done ! We used setter and getter to store the first result and reuse it in a console.log

Good to know : collectionVariables is not the only way to store data.

CollectionVariable = Manage data into the collection only
EnvironmentVariables = You can reuse data from one collection to another
GlobalVariables = Use it everywhere.



(Official Postman Documentation)

You can find the environment on the right corner at the top :

To see all your collectionVariables go there :

To see the other variable types see here :

In the next tutorial we will learn to use the
environmentVariables and how to store secret data

See all the official postman documentation here

Hope it helps !
Daphné Hervé (API, monitoring and QA automation lover)

Leave a Reply

Your email address will not be published. Required fields are marked *