pm.sendRequest的作用就是发送一个请求,并获取其他的返回信息。
pm.sendRequest可以在pre-Request Script 和Test模块中使用。
以下几种是:
1、发送get请求示例
2、发送json格式的Post请求示例
3、发送表单格式的Post请求示例
pm.sendRequest默认是不带参数的Get请求:
const url ="https://postman-echo.com/get"
pm.sendRequest(url, function (err, response) { // 参数1是request请求信息,参数2是请求回调返回的数据。
console.log(response.json());
});
使用pm.sendRequest发送JSON 格式的Post请求:
const regRequest = {
url: 'http://XXXX',
method: 'POST',
header: 'Content-Type: application/json', //注意要在Header中声明内容使用的类型
body: {
mode: 'raw', // 使用raw(原始)格式
raw: JSON.stringify({ name: 'Test', password: '123456' }) //要将JSON对象转为文本发送
}
};
//发送请求
pm.sendRequest(regR