As title, How can I access Request object in PostMan ? Is it possible to create a testcase like this
tests["Response content restaurant Id : ", req.body.restaurantId] = responseBody.has(req.body.restaurantId);
As title, How can I access Request object in PostMan ? 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
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. 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
This seems to work in latest Postman version (Thanks to tom redfern comment) ->
JSON.parse(pm.request.body.raw);
Because I was using the body as a template, using JSON.parse
directly on pm.request.body.raw
, wasn't working for me.
Using pm.variables.replaceIn()
to resolve pm.request.body.raw
did.
const body = JSON.parse( pm.variables.replaceIn(pm.request.body.raw))
© 2022 - 2024 — McMap. All rights reserved.