postman 在pre-request中發送application/x-www-form-urlencoded 格式表單


 

postman中在pre-request 發送請求

知識點:
  • json數據解析和遍歷
  • application/x-www-form-urlencoded表單
  • Array基本使用
  • js函數
  • http請求
  • postman全局變量
學習途徑
    • postman/jquery/js document
    • blogs

demo:

// 在請求傳的數據
var data = {
    test: '8888'
}


// json -- >  urlencode
function get_url_encoded_data(d){

    // urlencode存儲的變量,初始值為空串
    var encode_data = "";

    // temp array
    var temp = new Array();

    // 拼接
    for(k in d){
    temp.push(k + "=" + data[k])
    }

    for (i in temp){
        if (encode_data != ""){
            encode_data = encode_data + "&" + i
        }else{
            encode_data = temp[i]
        }

    return encode_data
    }
}

var url_encode_data = get_url_encoded_data(data)

console.log("url_encode_data", url_encode_data);

const echoPostRequest = {
  // demo addr, replace yours addr
  url: 'www.baidu.com',
  method: 'POST',
  header: 'application/x-www-form-urlencoded',
  dataType: "json",
  body: {
    mode: 'urlencoded',
    urlencoded: url_encode_data
  }
};
pm.sendRequest(echoPostRequest, function (err, res) {
  console.log(err ? err : res.json());
  // get person's id
//   pm.globals.set("id", res.json().person.id)
});

// 請求后等待15s,如果有函數內容,就在15s后先執行函數內容(可以再次判斷判斷,是否執行本次用例)
setTimeout(function(){}, 15000);

有疑問的地方可以加(641...069...4..69)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM