Accessing Collection Variables in Postman

社会主义新天地 提交于 2021-02-01 02:41:33

问题


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.


回答1:


Postman v7.9.0 added support for new pm.collectionVariables, so you can update them on test scripts:

pm.collectionVariables.set("collection_variable", newValue);

https://github.com/postmanlabs/postman-app-support/issues/5053#issuecomment-542555156




回答2:


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.




回答3:


Setting collection variables manually and then getting them was always possible.

Setting collection variables in scripts and not just manually became possible in version 7.9.0 which was released in October 2019. As of writing this, there is still a lot of obsolete misinformation about it out there - on the internet in general - but sadly also here at stackoverflow.

Although joseluislopez87 has already correctly answered the question, I am adding this answer in an attempt to help clear up any remaining confusion.


To find out who was right and who was wrong, I made a simple little experiment.
Below I describe what I did. I explain how you can replicate the exact same experiment yourself.

I have created a Postman collection by the name ManipCollVars
(ManipulateCollectionVariables seemed a little too long).
You can download and save it to your local drive from:
https://schulze.000webhostapp.com/postman/variables/ManipCollVars.pm_coll.json.

Then - from your Postman desktop app (not the chrome extension) - import ManipCollVars as shown in the figure below. (The GET request is https://postman-echo.com/get.)


To see the initial value of the collection variable CollectionVar, click on the three mini circles next to the collection name (tooltip: View more actions). Then click Edit. See the figure below.


In the figure below, notice how the CURRENT VALUE of CollectionVar is equal to Initial Value.
Click Cancel.


Click on the request ManipCollVars-Request, and then on its Tests tab as shown in the figure below.
Disregard the two tests and instead focus on lines 7-11:

// Will now try to change `CollectionVar` to some new value:
pm.collectionVariables.set('CollectionVar', 'Some New Value');
// Then print the new value of `CollectionVar` to the console:
console.log(pm.collectionVariables.get('CollectionVar'));
// ^^ Does `collVar` contain "Initial Value" or "Some New Value"?


Click on the blue Send button, and then open the Console in the bottom left corner. See the figure below. Notice how the value of the collection variable has changed from Initial Value to Some New Value. - Issue settled!


To double-check that the value has indeed changed, once again click on the three small circles next to the name of the collection, on Edit, and then on the Variables tab. See the figure below. Notice how the CURRENT VALUE of CollectionVar is now Some New Value. - Confirmed!


References:
https://learning.postman.com/docs/sending-requests/variables/#defining-variables-in-scripts
https://postman-quick-reference-guide.readthedocs.io/en/latest/cheatsheet.html#collection-variables
https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-collection-variables-in-scripts
https://stackoverflow.com/a/58325002
https://blog.postman.com/postman-the-easy-way-to-work-with-apis/



来源:https://stackoverflow.com/questions/47680580/accessing-collection-variables-in-postman

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!