一、.需求1:
postman可以在接口请求Pre-request 添加请求前的操作,很多接口都是依赖于先登录的。于是可以在Pre-request 发送一个登录请求获取token。
添加请求的代码:
这里在获取token之后更新到请求头部,有两种写法:
写法1:
const loginRequest = {
url: 'http://xxx',
method: 'POST',
header:
['Content-Type: application/json',
'source: xxx',
],
//没有source可以不填,去掉大括号
body: {
mode: 'raw',
raw: JSON.stringify({ username: 'xxx', password: 'xxx' })
}
};
pm.sendRequest(loginRequest, function (err, res) {
if (err) {
console.log(err);
} else {
// 读取接口返回的 json 数据。
// 如果你的 token 信