How can I use Postman Collection Variables? - postman

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.

Related

Cannot specify path variable on a Flow / Send Request

I was playing with Postman Flows, and I was trying to learn by using the Trello API. All requests work on their own if executed manually. I've also debugged values using the terminal to understand where the problem lies. Before that, here's a summary of what I'm doing.
Get all boards for a given trello workspace
For each board, delete that board.
The complete flow looks like this:
I've checked that on the last block Send Request, the looped value of /variable/id outputs the proper board id. I've done this by checking with a terminal block and a string block. I started suspecting that this is caused by a failure of Postman to understand that the variable I'm trying to use is a path variable and not a query parameter. As such I tried to pass a static value to the Send Request and it 404'ed as well (tech aside: in theory for n ids it should give me one 200 and n-1 404s since the variable is static and the board would not be able to be deleted multiple times).
My suspicion comes from the fact that when configuring the block for this request:
You do not get prompted to add the board variable. I've tried to type it in anyway, and even use combinations like :board, with no avail. In fact like I said above, if I use these variables with static values, it still 404s.
ignore the parsing message on the right hand side...
As you can see, board doesn't show up. Did I end up hitting a bug, or is this user error? One thing I do not know how to do, but would help clarify that the issue is that a null value is being passed on to the DELETE would be to output the request itself. On a terminal block I can only see the response.
Thanks in advance.
UPDATE:
After checking the Postman console on the app, I've noticed that in fact the path variable being used is whatever is set on the collection request. It's like it takes the URL as a static field and disregards the path variables. Any thoughts?
Path variables won't be available in your Send Request. Instead, define your path variable with an environment/collection/global variable (i.e. {{board}}) in the value of the path variable. Then it will show up the relevant block of your flow.

Postman Request Chaining by Setting Next-Token Header Variable

I have a GET Request that returns a fixed paginated number of results. To process all of the pages, I would need to use the available X-Next-Token.
I have the X-Next-Token set as a an empty variable in the environment.
In Postman's Tests Section, I can see that I can pull the X-Next-Token
Tests
next_token = pm.response.headers.get("X-Next-Token");
console.log(next_token)
postman.setEnvironmentVariable("X-Next-Token", next_token);
console.log(pm.request.headers)
Postman Console
I've tried to this solution
postman.setEnvironmentVariable("X-Next-Token", pm.response.headers.get("X-Next-Token"));
When the X-Next-Token variable is empty it seems to populate initially. But it doesn't change subsequently.

How to change Postman current variable value in collection scope?

I have variable "TOKEN" in my collection scope. I try to set the value using Tests scripts when do a request. But the variable not changed.
So, I try to use environment scope variable. And It works.
Why It's doesn't work when in collection scope? I had read about postman variable scope here and understand it well.
Here're some screenshots:
1. First, I call login endpoint.
Below is the console result. Nothing wrong.
Until I try to get all users endpoint that required token in request header. The status is 401 because the token is null. If the token is not null, then it will return 200:
It's gracefully working when I add "TOKEN" variable to environment. Switch to No Environment again will result 401 status code:
This may be new since this question was posted, but for anyone else
finding this, you can set collection variables using:
pm.collectionVariables.set(key, value)
See:
https://learning.postman.com/docs/sending-requests/variables/#defining-variables-in-scripts
You can now use:
pm.collectionVariables.set("variable_key", "variable_value");
In my original answer in June 2019, I wrote that collection variables are not editable through scripts, and can only be changed manually. As noted, this is no longer the case. . . .*
Turn off Automatic persist variable values from postman settings.
image
It seems you can only set env variables when some environment is selected: https://learning.postman.com/docs/postman/scripts/postman-sandbox/#environment-and-global-variables

Postman API -how to pass environment variable to another request jason body

I have create global variable. Set it as Test as env variaable corresponding quote id were stored in the CreateGLVar
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);});
var jsonData = pm.response.json();
pm.environment.set("CreateGLVar",jsonData.result.quoteID);
script for storing the value in Env variable
May i know how i can use value which is stored in the CreateGLVar for the below script. how i can the quote id from first request from global variable and insert dynamically in the second request( shown below) .
get quote id
enter image description here
Postman uses double curly braces to insert variables, which can also be used in raw request bodies.
In your specific case you can use:
"quoteID": "{{quoteIdVariable}}"
I am using the Postman Chrome extension Version 5.3.1, and this works for me.
Edit: Now that the Chrome extension has been depricated, this still works with the Postman Desktop app
Thanks Aaron.
I got the success when i used the "quoteID": "{{quoteIdVariable}}" in my bind API.my 2 API are working fine when i executed individually.
But I got issue when i executed API's as collection( Quote and Bind API). What i missing here if i executed as collection.
Failed

What are the "local" variables in Postman?

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