一、背景
由于很多項(xiàng)目,都會依賴某一個(gè)接口的數(shù)據(jù),才能運(yùn)行其他接口,所以可以在其他接口的前置腳本中,編寫依賴的接口請求,進(jìn)行調(diào)用,這樣可減少依賴造成的不便。
二、發(fā)送請求的類型
通過sendRequest()方法發(fā)送
Get請求
定義常量
發(fā)送get請求
const url = ''//get請求的url地址及參數(shù)
pm.sendRequest(url, function (err, response) { //err為異常,固定寫法;response為響應(yīng)參數(shù),可隨意定義
console.log(response.json());//響應(yīng)內(nèi)容輸出到控制臺
});文章來源:http://www.zghlxwxcb.cn/news/detail-742602.html
Post請求
構(gòu)造一個(gè)請求
發(fā)送post請求
const loginRequest = {
url: '',//post請求地址
method: "POST",
body: {
mode: 'urlencoded', // 模式為表單url編碼模式
urlencoded: 'name=張三&password=123456' //參數(shù)需要轉(zhuǎn)義
}
};
pm.sendRequest(loginRequest, function (err, response) {
console.log(response.json());
});
JSON格式請求
構(gòu)造一個(gè)post請求
發(fā)送請求
const regRequest = {
url: '',//post接口地址
method: 'POST',
header: 'Content-Type: application/json', //注意要在Header中聲明內(nèi)容使用的類型
body: {
mode: 'raw', // 使用raw(原始)格式
raw: JSON.stringify({ json文本參數(shù) }) //要將JSON對象轉(zhuǎn)為文本發(fā)送
}
};
pm.sendRequest(regRequest, function (err, res) {
console.log(err ? err : res.json()); // 響應(yīng)為JSON格式可以使用res.json()獲取到JSON對象
});
XML格式請求
構(gòu)造請求
發(fā)送請求
const demoRequest = {
url: '',//post接口地址
method: 'POST',
header: 'Content-Type: application/xml', // 請求頭格式
body: {
mode: 'raw',
raw: '<xml>hello</xml>' // xml轉(zhuǎn)換為文本
}
};
pm.sendRequest(demoRequest, function (err, res) {
console.log(err ? err : res.json());
});
三、更改接口請求順序文章來源地址http://www.zghlxwxcb.cn/news/detail-742602.html
到了這里,關(guān)于postman發(fā)送請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!