DEV Community

Cover image for [Postman] The variables
Daphné Hervé
Daphné Hervé

Posted on

[Postman] The variables

From advanced to expert 
For developers and testers.  
         ---------          
Some great tutorials to get all the tips of Postman :
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Feel free to enter the adresse of your choice ;)

Image description

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

Image description

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))
Enter fullscreen mode Exit fullscreen mode

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

Image description

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);
Enter fullscreen mode Exit fullscreen mode

Super nice ! And we add a getter :

// Getter
let firstAdresse = pm.collectionVariables.get("adresse_label_variable");
console.log('stored_variable', firstAdresse);
Enter fullscreen mode Exit fullscreen mode

Should be like this :

Image description

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.

Image description
Image description
(Official Postman Documentation)

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

Image description

To see all your collectionVariables go there :

Image description

To see the other variable types see here :

Image description

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)

Top comments (0)