Postman is a platform used by developers, API testers, technical writers and DevOps teams for testing, documenting and collaborating on API development. It provides a user-friendly interface for making different types of API requests (HTTP, GraphQL, gRPC), inspecting responses, and organizing API calls into collections for collaboration and automation.
Performing repetitive tasks while testing APIs is stressful and time-wasting. For example, the process of retrieving, copying and pasting new authentication tokens for use in Postman is repetitive. You can simplify this process by using Postman scripts to store auth tokens and then reuse them without repeating the copy and paste steps.
To practice along with this guide, you should have:
The Postman API client installed on your computer
Experience in making API requests with Postman
A backend application that uses JWT authentication and has its documentation in your Postman client
If you don’t have a backend application setup, I created one that you can clone from GitHub at orimdominic/freeCodeCamp-postman-api-jwt.
By the end of this article, you should be able to simplify the process of obtaining and reusing authentication tokens across your API requests. You should also have a practical understanding of some scripts necessary for use in other areas of software testing with Postman.
Table of Contents
What are Postman Scripts?
Postman scripts are blocks of JavaScript code that you can write and run within the Postman API client to automate and enhance API testing workflows. You can use Postman scripts to add code to run before and after API requests. These scripts can be used to:
Add logic and process data from API requests
Write test assertions for API responses
Run automated tests on API endpoints
You can find Postman scripts under the Scripts tab of an API request. Code written in the Pre-request tab runs before the request is made and code written in the Post-response tab runs after the response is made.
How to Simplify Your JWT Authentication Process
In summary, you will carry out the following steps to achieve the objective of this tutorial:
Authenticate to get the token
Save the token in a collection variable with Postman scripts
Use the variable in an API request
Authenticate to Get the Token
To get started, carry out the following steps:
Start your backend application and make sure it is running successfully.
Open up your Postman application and go to the API request for signing in to get a JWT.
Make an API request to the sign in endpoint and take note of the JSON response schema.
The highlighted part of the image above shows the JSON response from a successful sign in request. In the response schema, the auth token to be used for authorization is in the data.token
field. You will use Postman scripts to store this token in a variable and then use the variable in the Authorization
header of requests that require authorization.
How to Save the Token in a Variable with a Postman Script
In Postman, click on the Scripts tab next to the Body tab. If the Postman application window is small, you may need to click a dropdown to see it. Next, click on the Post-response tab. In the text area to the right, you will write the script to capture the auth token from the response and store it in a Postman variable. Copy the JavaScript code below and paste it into the text area.
<span class="hljs-keyword">if</span> (pm.response.code == <span class="hljs-number">200</span>) {
<span class="hljs-keyword">const</span> token = pm.response.json().data.token
pm.collectionVariables.set(<span class="hljs-string">"auth_token"</span>, token)
}
Postman scripts use the pm
identifier to access and modify information in the Postman environment. The script above uses pm
to first ensure that the request was successful by checking if the response status code is 200
.
Inside the conditional statement, pm.response.json().data.token
is used to get the authentication token from the JSON response and store it in a collection variable called auth_token
. If auth_token
doesn’t exist already, it is created and its value is set to the value of token
. If it exists already, its value is replaced.
To confirm that auth_token
has been set, click on the name of the collection (labelled 1 in the screenshot above) and then click on the Variables tab (labelled 2 in the screenshot above). Next, instead of repeatedly copying the token and pasting it in the Authorization
header of your requests, you will use auth_token
in the Authorization
header of your requests.
How to Use the Variable in a Request
Reference the collection variable in the Authorization
header by surrounding it with double curly braces {{auth_token}}
. When you make an API request, Postman will use the value referenced by {{auth_token}}
as the Authorization
header.
If another authentication request causes the value of auth_token
to be updated, you no longer need to copy the new auth token. The script in the post-response tab will update the auth_token
value and you can go on with making API requests smoothly. No need for repeatedly copying and pasting – Don’t Repeat Yourself (DRY).
Next Steps
In this tutorial, you have learnt how to use Postman scripts to set environment variables in Postman. You have also learnt how to eliminate the process of repeatedly copying and pasting auth tokens for use in API requests.
For guides on writing assertion tests for your APIs, check out the Test API Functionality and Performance in Postman guide by Postman.
Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & MoreÂ