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)

猜你喜欢

转载自www.cnblogs.com/chenadong/p/11374718.html
今日推荐