javascript 如何在 PostMan 中访问 Request 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33913701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access Request object in PostMan
提问by Trung
As title, How can I access Request object in PostMan ? Is it possible to create a testcase like this
作为标题,如何访问 PostMan 中的 Request 对象?是否可以创建这样的测试用例
tests["Response content restaurant Id : ", req.body.restaurantId] = responseBody.has(req.body.restaurantId);
回答by Trung
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;
回答by Harald Nordgren
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);
});
回答by A.te
//this works for form-data:
var reqBody = request.data;
//this works for raw:
var reqBody = JSON.parse(request.data);
回答by taurius
for application/json request body, you would use the answer provided by 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
对于 application/json 请求正文,您将使用 Trung 提供的答案。但是,对于表单数据,您只需使用 request.data 访问正文,然后您就可以直接获取您的变量,例如 request.data.email 或 request.data.password