Currently, it is possible to set and get variables from the global and environment scope, as well as the generic variable in a pre-request script. However, the documentation is not clear if it is possible to programmaticaly set collection scoped variables.
For example
pm.environment.set("timestamp", timestamp); //acceptable
pm.global.set("signature", hash); //acceptable
pm.variable.set("signature", hash); //acceptable
pm.collection.set("signature", hash); //not possible?
Is this possible?
You can only currently set these manually at the Collection level but you can reference these using the pm.variables.get('var_name') syntax.
https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables#defining-collection-variables
EDIT:
Postman now allows you to use:
pm.collectionVariables.set('var_name', 'var_value') and pm.collectionVariables.get('var_name') to interact with the Variables at the Collection level.
https://stackoverflow.com/a/58325002/6028443
Related
Is it possible to use variables in the description of a request in Postman?
I have created a collection, and want to use a variable in the description.
My main goal is to create json schemas for defining object types, so I thought I would store global variables with the json schema, and reference those types in the description of a request or collection. Is that possible?
At the moment it isn't possible to use variables in the description of a collection. You can always submit a feature request here:
https://community.getpostman.com/c/feature-requests?order=views
I didn't see a current request for that particular feature, perhaps others would be interested as well.
How to pass multiple variables as Header Request in the postman?
You can do something like this as follows-
Create environment variables in your postman. If you dont know how
to create environment variables the use this link.
Use the variables in your Header like this-
Benefit of the environment variables is you can use multiple variables and its values and it optimze your work efforts.
Hope this helps.
When I add environment variables I can use them in my post body with {{varName}}. But this does not work for collection variables (Collection > edit > Variables tab)
With the settings as shown above, if I add {{firstName}} to my body it does not work. How can I access these collection variables in my posts?
Currently if I try to post postman will just hang for a while then give this error
Error: Script execution timed out.↵ at
ContextifyScript.Script.runInContext (vm.js:53:29)
If I use an environment variable or just type in a value it works fine.
Also, you need to make sure to save the request to the belonging collection before you can use it!
It turns out {{varName}} does work. The problem was in my pre-request script. The API I was connecting to requires a checksum on the body so it pre-processes the variables in the body, but it was not setup to handle collection variables. This was causing postman to fail. User error.
I have the following setup:
A global variable api_address defined as http://apiproduction.mydomain.com.
A collection variable route_address defined as users/profiles for each collection.
Right now my query look like: {{api_address}}/{{route_address}}/004ba492-d021-40fe-ba23-f1d366036af4 if i want to get the resource.
So my question is... Could you include in a collection variable a global one?
Example: {{route_address}} could be defined as {{api_address}}/users/profiles.
Yes you can do this! But you have to do it with pre-request scripts.
Edit your selection and go to the pre-request script tab.
Add something like the following:
let api_address = pm.globals.get("api_address");
pm.variables.set("route_address", `${api_address}/users/profile`);
That should give you your "route_address" that you are looking to variablize.
You can probably to the pm.globals.get inline on your set, but I'm old school and think it's cleaner and easier to read if you throw it into a local variable first.
Postman's documentation leaves a lot to be desired. In their Variables page they say:
The following scopes are available to you:
Global
Environment
Local
Data
There's information about the Global and Environment scopes, and I believe the "Data" scope is the data from a collection run. But what are the "local" variables?
Because I'd love to have a variable that is calculated on the fly, used for the request, and then discarded. Both global and environment variables are persistent.
According to the Postman Quick Reference Guide local variables are only available within the request (or collection run) that has set them. So they are used for the request or collection run and then discarded.
When to use:
passing data from the pre-request script to the request or tests or between requests.
The behavior is a bit different in Postman vs Collection Runner / Newman, so make sure you understand how they work before using!
Setting
pm.variables.set('myVariable', MY_VALUE);
Getting
pm.variables.get('myVariable', MY_VALUE);
Removing
Local variables are automatically removed once the tests have been executed / collection run finished.
Local variables are the one you use in your Tests part.
You may even use the 'let' declaration as it is coded in javascript ...
ie:
let jsonData;
jsonData = JSON.parse(responseBody);
or use var for declaration.
var jsonData = JSON.parse(responseBody);
Though, you can erase globals on the fly using
pm.environment/global.unset(<variable>)
see here for details