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)
