[英]How can I access Request object in PostMan
As title, How can I access Request object in PostMan?如标题,我如何访问 PostMan 中的请求 object? Is it possible to create a testcase like this
是否可以创建这样的测试用例
tests["Response content restaurant Id : ", req.body.restaurantId] = responseBody.has(req.body.restaurantId);
After doing some research in Postman Sandbox在Postman Sandbox 中做了一些研究之后
I finally found the answer for myself.我终于为自己找到了答案。
var reqBody = JSON.parse(request.data);
var resBody = JSON.parse(responseBody)
tests["Data"] = reqBody.restaurantId === resBody.restaurantId;
If you are doing it from a test script this is the syntax:如果您是从测试脚本执行此操作,则语法如下:
pm.test("Update env", function () {
var req = JSON.parse(pm.request.body.raw);
pm.environment.set("restaurantId", req.restaurantId);
var resp = pm.response.json();
pm.environment.set("restaurantId", resp.restaurantId);
});
//this works for form-data:
var reqBody = request.data;
//this works for raw:
var reqBody = JSON.parse(request.data);
for application/json request body, you would use the answer provided by Trung.对于 application/json 请求正文,您将使用 Trung 提供的答案。 However, for the form data, you need to simply access the body using request.data and then you can get your variables directly, like request.data.email or request.data.password
但是,对于表单数据,您只需使用 request.data 访问正文,然后您就可以直接获取您的变量,例如 request.data.email 或 request.data.password
This seems to work in latest Postman version (Thanks to tom redfern comment) ->这似乎适用于最新的 Postman 版本(感谢tom redfern评论)->
JSON.parse(pm.request.body.raw);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.