Postman深入解析之-pm.sendRequest

話不多說,直接上乾貨
postman 官方文檔:https://learning.getpostman.com/

在很多請求之前都需要先獲取token,本文以OAuth 2.0爲例,

  1. 在header標籤欄加上 Bearer {{oa2_token}}

  2. 進入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”);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章