Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Collection Variables in Postman

Tags:

Feature

Postman added support for variables, authorization, pre-request and test scripts to collections. (as of version 5.4.1 this exists at both the collection AND folder level)

Use case

Let's say I want to store a refresh token when the login endpoint is hit. My test script needs to create/update a COLLECTION variable, NOT a global or environment variable.

Once that refresh_token is available to the collection, other tests and pre-request scripts, I would think there is a way to access them through an API similar to pm.environment or pm.globals. (pm.collection, for instance)

Question

I cannot find any documentation on how to access or modify those via pre-request scripts, or tests... does anyone know how to do this? Maybe this hasn't been thought out completely, or not fully implemented, but I thought I would check with others for some help.

Temporary Solution

As a complete hack, I am storing the things I need as namespaced environment variables. It's not ideal (makes things kind of messy when working in other collections) but it works just fine.

like image 531
Ed Meacham Avatar asked Dec 06 '17 17:12

Ed Meacham


People also ask

How do I see all variables in my Postman?

For getting the global variables the feature request is in-progress in GH. Once you fork the Postman API's, you have Collection folder. Here you have the section “variable” which will lists all the collection variables.

Where are global variables in Postman?

To view global variables, click the Quick Look icon in the upper right corner of the Postman app to display the environment and global variables. When you click the “Edit” link, it opens a modal for editing keys and values.


1 Answers

Collection variables

You can access collection variables (and all variables) in the pre-request and test script sections using pm.variables.get("variableName").

However, you can only define and update collection variables by editing the collection details via modal.

Note: For your current solution using environment variables getting messy, remember you can always use pm.environment.set() to reset the value or pm.environment.unset() to clear it.

like image 132
J.Lin Avatar answered Sep 22 '22 17:09

J.Lin