Postman is a powerful graphical HTTP client helpful when
working with APIs. In our day to day work, we use postman for our REST Service
tests a lot. In this post I am going to discuss about few tips and tricks while
using postman.
1. Variables:
We can use variables to replace certain
fields such as the URL, URL parameters, headers, authorization, request body,
and header presets etc. in postman. These variable names are enclosed with
double curly braces – like {{variable name}}. Postman uses string substitution
to replace the current value of this variable.
There are multiple ways to initialize,
retrieve, and define the scope of these variables. Below is the practical usage
of this feature.
How I used variables:
The
most annoying problem we face in our everyday usage of postman is, updating the
Auth token. As the auth token has limited validity, we have to replace the
token every time when it gets expired. This hack helps the users to include
this token generation in the run time, so that we don't need to update every time.
First, we need to add the below script under "Pre-request Script" tab. (The Pre-request Script is a tab where one can write javascript code to hit any endpoint and extract data from a response to use this data in a subsequent request. So, if you include any script in this tab, this will be executed even before making your REST Call. Thus it initializes & loads the variable even before making your actual Call.)
const echoPostRequest = {
url: 'http://ur.access.token.endpoint',
method: '<GET or POST>',
header: 'Content-Type:application/json'
};
var getToken = true;
console.log(pm.sendRequest);
if (getToken === true) {
pm.sendRequest(echoPostRequest, function (err, res) {
console.log(err ? err : res.json());
if (!err) {
pm.globals.set("token",
res.json()["<field name of the response where your token is
stored>"]);
}
});
}
In the above script, we are storing the token
value in the variable called “token”. Now we use this variable in the header as
the value of “Authorization” like below (You may be using different key for
authorization. Use the same key here).
That's it. Now you can run this
collection, whenever you want without worrying about replacing latest token.
2. Dynamic Variables:
Postman has a few dynamic variables you can
use in your requests. We can use them in the request builder as unique IDs,
timestamps, or random integers. The syntax will be {{$timestamp}}. Below are those variables and their purposes.
{{$guid}} : Adds a v4 style guid
{{$timestamp}} : Adds the current timestamp
{{$randomInt}} : Adds a random integer
between 0 and 1000
3. Data Variables:
Data variables are available when using a
data file with the collection runner. You can import the data as a CSV or JSON
file, and then use the values from the data file inside HTTP requests and
scripts. Here's an example of Inside pre-request and test scripts. Let's say
you have the pm.iterationData.get("username") method inside
pre-request and test scripts. The method would let you access the value of the
username variable from a data file.
You can get more details on how to use data variables from the below link
No comments:
Post a Comment