33

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);
Xan
  • 74,770
  • 16
  • 179
  • 206
Trung
  • 1,372
  • 2
  • 14
  • 21

5 Answers5

61

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;
Trung
  • 1,372
  • 2
  • 14
  • 21
  • var reqBody = request.data; works for me - with JSON.parse() I got an error – o.z Jul 24 '18 at 12:20
  • 1
    It depends on whether you're using the form-data or the raw JSON body to make the request @o.z. In case you never figured it out (I realize it has been some time). – Kreidol Apr 03 '23 at 23:40
23

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);
});
Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
5
//this works for form-data:
var reqBody = request.data;
//this works for raw:
var reqBody = JSON.parse(request.data);
A.te
  • 51
  • 1
  • 2
1

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

0

This seems to work in latest Postman version (Thanks to tom redfern comment) ->

JSON.parse(pm.request.body.raw); 
Vidhya
  • 79
  • 2
  • 6