About using postman to set the global token to automatically add to the request header when requesting

When using the postman joint debugging interface to initiate a request, many interfaces have the same part. For example, request the root path and the token carried in the request. How to put the request root path inpostmanConfigure in one place, use everywhere? How to bring it yourself on every requesttoken, do not need to write token in each request header?

1. Understand the variables of postman

Postman variables are divided into global variables and environment variables

1. Global variables

Variables that take effect globally, the only

1. Set global variables

code settings

	pm.globals.set("variable_key", "variable_value");

interface settings

Find the global variable to set the name, initial value, and current value of the global variable

After the setting is complete, click the upper right corner of the software to view the current variables

2. Using global variables

code usage

pm.globals.get("variable_key");

interface usage
Wrap it in double curly braces and use it directly in the postman interface

2. Environment variables

Take effect in the specified environment (development, test, production).

1. Set environment variables

code settings

pm.environment.set("variable_key", "variable_value");

interface settings

After the setting is complete, select the set environment variable, and you can also view it

2. Using environment variables

code usage

pm.environment.get("variable_key");

interface usage
Wrap it in double curly braces and use it directly in the postman interface

used in the project

1. Store the token in the environment variable, and use the environment variable for each requested token value

In the above, you can set in the environment variabletokenThis variable, every request addstokenwhen, willtokenThe value of is set to{{token}}, you can use the token in the environment variable.

2. Store the token in the environment variable, and automatically add the token to the request header before each request

This will be incollectionsofpre-requestofScriptIn each request will betokenadded to the request header.

At this time, in each request, there is no need to add token in the request header

iftokenis inAuthorizationcan be directly incollectionsneutralAnthorizationfill in, not inpre-requestmedium-length script

per requestAuthorizationChoose inheritance to achieve each request usingcollectionsneutralAuthorizationoftoken

2.1. The request root path can also be automatically added before the path of each request in the same way as the token

part of the code

if (pm.environment.get("token")) {
	pm.request.headers.add({
    	key: 'token',
        value: pm.environment.get("token"),
    })
    console.log(pm.request)
    pm.request.url.host  =pm.environment.get("base_url") 
}else {
    console.warn("No token")
}

3. Write the login interface into postman, and store the token of successful login directly into the environment variable

This is an example of successfully logging in through the verification code and obtaining the token

The token obtained by the login interface can be directly written into the environment variable.

You can debug in postman, just click the console in the lower left corner of the software

If there is any mistake, welcome to correct me, thank you!
The above content is for reference only, welcome to discuss.

Related Posts

How to conduct CAN bus efficient test?

vue+el-upload (encapsulate Huawei Cloud OBS upload file) front-end direct upload

React Native basics

The Vue project keeps the user login status (localStorage + vuex keeps the status after refreshing the page)

In-depth Vue principle_Comprehensive analysis of publish and subscribe mode

BurpSuite – proxy and browser settings

transition of vue3

[JS] Get today of the next month

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*