话不多说,直接上干货
postman 官方文档:https://learning.getpostman.com/
在很多请求之前都需要先获取token,本文以OAuth 2.0为例,
-
在header标签栏加上 Bearer {{oa2_token}}
-
进入Pre-Request Script,
const client_id = “FGlZYYrUCAFm3rJv86pn5N1dFGilU8lL”;
const client_secret = “4ArhUB0o1faSDGNv”;
const basic_token = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(client_id + “:” + client_secret));const regRequest = {
url: ‘your URL’,
method: ‘POST’,
header: [‘Content-Type: application/x-www-form-urlencoded’,
'Authorization:Basic ’ + basic_token],
body:
{
// mode: ‘raw’, // 使用raw(原始)格式
// raw: JSON.stringify({ grant_type: ‘client_credentials’ }) //要将JSON对象转为文本发送
mode: ‘urlencoded’,
urlencoded: [{
“key”: “grant_type”,
“value”: “client_credentials”,
}]
}
};pm.sendRequest(regRequest, function (err, response) {
console.log(response.json())
pm.variables.set(“oa2_token”,response.json().access_token);
});// postman.setNextRequest(“next_request_name”);