简体   繁体   English

如何在 PostMan 中访问 Request object

[英]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 SandboxPostman 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.

相关问题 如何在 postman 中发送 object 数组? - How can i send array of object in postman? 如何访问 Postman 中的 window object - How to access the window object in Postman 如何访问环回操作挂钩中的请求对象? - How can I access request object in the loopback operational hooks? 如何在 postman 测试中检查请求正文是否有密钥 - How can I check request body has a key or not in postman tests 如何在 Postman 中创建动态变量以供进一步访问? - How can I create a dynamic variables in Postman for further access? 如何访问 Postman 中的原始数据(请求部分)? - how to access raw data in Postman(Request section)? 我如何在Mongoose Virtual Get Func中访问快递请求对象 - How can I access express request object in Mongoose virtual get func 如何创建可以在邮递员请求中多次使用的生成器函数? - How do I create generator function that can be used multiple times in a postman request? 在邮递员中,无论大小写或数组顺序如何,我如何检查响应中的值是否与请求的值相同? - In postman, how can i check if the values in the response is same as that of the request irrespective of case or the order for an array? 如何在预请求和测试中访问邮递员中的请求URL /标题? - How to access Request URL/Header in Postman in Pre-Requests and Tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM