[Interface test] [postman] postman obtains the token through a script and automatically adds the request header to realize the parameterization of the token

table of Contents

One demand background

Two realization plan

Solution 1: Get Token from URL and manually fill in the request header

1.1 Get Token value through URL

1.2 Headers manually fill in the Token in the request header and send the request

Scenario 2: The script obtains the token and automatically writes it to the request header

2.1 Enter the Pre-request Script script page

2.2 The script realizes the Token parameterization and automatically adds it to the request header

2.3 Execute request to obtain data

Three detailed scripts

3.1 Script to implement Token parameterized code framework

3.2 Token parameterized script example

Four references


 

One demand background

We need to perform Token verification when accessing the web page (the Token value can be obtained by request), and when sending a data request, we need to append the Token to the request header for verification in order to obtain the requested data normally. Due to security and other design considerations, Token cannot be valid for a long time, so it has a validity period. After expiration, it can no longer be applied and needs to be re-obtained. If the Token validity period is short or the project development/test cycle is long, you need to manually obtain the latest Token and then manually update it every time, which is obviously time-consuming and laborious.

So, is there a work-for-all method that can automatically write the dynamically changing Token into the request header ?

Of course there is! In addition to simple post, get, set and other requests, Postman also has many tall functions. Let's introduce its implementation in detail...

 

Two realization plan

Solution 1: Get Token from URL and manually fill in the request header

1.1 Get Token value through URL

1.2 Headers manually fill in the Token in the request header and send the request

Scenario 2:脚本获取Token并自动写入请求头

2.1 EnterPre-request Script脚本页面

①Select the project folder or request (the folder is valid for all requests in its subdirectories), right-click and select Edit:

②Switch the pop-up Edit form to Pre-request Script 页面

2.2 The script realizes the Token parameterization and automatically adds it to the request header

2.3 Execute request to obtain data

① Request data 1

Note: Token needs to be checked in the request header, otherwise the Token obtained by the script will be overwritten due to scope issues, which will cause the request to fail.

② Request data 2

Three detailed scripts

3.1 脚本实现Token参数化代码框架

pm.sendRequest({ url: "https://{your url to get token}", //Get the token url method:'POST', //The request type is POST header: {//Request header'Accept': 'application/json',                          'Content-Type':'application/json', }, body: {//Request body mode:'raw', raw: JSON.stringify({ username: "username", password: "password "}) //Authorization request account } }, function (err, res) { pm.request.headers.add({ key:'Token', value: res.json().access_token }) // Extract from Response Body Token value, And add it to Headers });












3.2 Token参数化脚本实例

 

Four references

https://umm.js.org/p/c23ff637/

 

 

If the article is helpful to you, remember to like , bookmark, and follow . I will share some dry goods from time to time ...~~~///(^v^)\\\~~~

 

Guess you like

Origin blog.csdn.net/u010521062/article/details/114941970