发送get请求
const url = 'http://115.28.108.130:5000/api/user/getToken/?appid=136425';
// 发送get请求
pm.sendRequest(url, function (err, res) {console.log(err ? err : res.text()); // 控制台打印请求文本
});
发送表单格式post请求
//构造一个登录请求
const loginRequest = {url: 'http://115.28.108.130:5000/api/user/login/',method: "POST",body: {mode: 'urlencoded', // 模式为表单url编码模式urlencoded: 'name=张三&password=123456'}
};// 发送请求
pm.sendRequest(loginRequest, function (err, res) {console.log(err ? err : res.text());
});
发送json格式请求
// 构造一个注册请求
const regRequest = {url: 'http://115.28.108.130:5000/api/user/reg/',method: 'POST',header: 'Content-Type: application/json', //注意要在Header中声明内容使用的类型body: {mode: 'raw', // 使用raw(原始)格式raw: JSON.stringify({ name: '小小', password: '123456' }) //要将JSON对象转为文本发送}
};//发送请求
pm.sendRequest(regRequest, function (err, res) {console.log(err ? err : res.json()); // 响应为JSON格式可以使用res.json()获取到JSON对象
});
发送xml格式请求
//构造请求
const demoRequest = {url: 'http://httpbin.org/post',method: 'POST',header: 'Content-Type: application/xml', // 请求头中指定内容格式body: {mode: 'raw',raw: '<xml>hello</xml>' // 按文本格式发送xml}
};//发送请求
pm.sendRequest(demoRequest, function (err, res) {console.log(err ? err : res.json());
});
参考文章