Accessing GET request "Response Body" in Pre-request Script

I am trying to confirm the functionality of POST requests to a hardware device. To do this I want to get the current state of the hardware first by using a GET requests in the “Pre-requests Script” section.

My code in there is:

pm.sendRequest("", function (err, response) {
    const GETpreRequest = pm.response,json();

Which gives me the error: “TypeError: Cannot read properties of undefined (reading ‘json’)”

When I look at the type for pm.response it shows it as undefined, but looking in the console the response bogy looks to be correctly formatted json.

17 ms
Request Headers
User-Agent: PostmanRuntime/7.36.3
Accept: */*
Postman-Token: 47352790-0aed-4dc8-910d-d2dcfa859a71
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Response Headers
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods: GET,HEAD,OPTIONS,POST,PUT
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Wed, 28 Feb 2024 08:09:16 GMT
Content-Length: 279
Response Body
concole.log of pm.responce gives me: undefined

When I use similar code in the “Tests” section of this POST request I get the response I would expect.

I am new to javaScript, and scripting in Postman, so I may have missed something obvious. If somebody could tell me why I can’t access the json in the response body (or if there is a better method of doing it) that would be fantastic.



It is the use of pm.response.json() which should have been just response.json. I found the information in this article.

Send an asynchronous request

You can send a request from your test code and log the response:

pm.sendRequest("", function (err, response) {

I still don’t know the why of this, so if you know where to find documentation of that it would be helpful, or you can just give me the answer :stuck_out_tongue_winking_eye:

This is really a JavaScript question as the Postman sandbox uses JavaScript under the hood.

The pm object provides functionality for testing your request and response data.

This object is broken down into methods.
pm.sendRequest() is one method, and pm.response() is another.

The details on the methods are here…

Manage API data and workflows using Postman JavaScript objects | Postman Learning Center

For example, the pm.sendRequest() method accepts a request and a callback.

Send asynchronous requests with Postman’s PM API | Postman Blog

The callback receives 2 arguments, an error (if any) and SDK compliant response.

In your case, you have called those two arguments “err” and “response”. You could have called those variables anything you like. I usually call mine “res” when using sendRequest() to avoid any conflict with the main response where you might be using the same variable name.

The response will have more than just the body. It will also include the headers, and status code for example.

The resulting response variable has a further method called json() which allows you to parse the response body.

pm.response.json() is similar but its a special method linked to the core pm.response function and works against standard requests.

Another method for parsing the response is JSON.parse() which is a standard JavaScript function and used to be the way you had to parse the response before the pm.response.json() method become available to make things easier.

Thanks for the detailed answer here, this really does clear up my question, and I will put it to use straight away.

@michaelderekjones I’ve also seen some of your other responses as I have been searching around the Postman help pages and they too have been especially useful. Keep up the excellent work!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.