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 inpostman
Configure 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 variabletoken
This variable, every request addstoken
when, willtoken
The 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 incollections
ofpre-request
ofScript
In each request will betoken
added to the request header.
At this time, in each request, there is no need to add token in the request header
iftoken
is inAuthorization
can be directly incollections
neutralAnthorization
fill in, not inpre-request
medium-length script
per requestAuthorization
Choose inheritance to achieve each request usingcollections
neutralAuthorization
oftoken
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.