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

**URL:** <https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214>\
**Category:** Help Hub\
**Tags:** tests, json\
**Created:** [February 28, 2024, 12:25am UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214 "2024-02-28T00:25:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [February 28, 2024, 12:25am UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214/1 "2024-02-28T00:25:57Z")

</div>

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:

```auto
pm.sendRequest("http://192.168.20.150:8080/picsetup", 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.

```auto
 
GET http://192.168.20.150:8080/picsetup
200
17 ms
Network
Request Headers
User-Agent: PostmanRuntime/7.36.3
Accept: */*
Postman-Token: 47352790-0aed-4dc8-910d-d2dcfa859a71
Host: 192.168.20.150:8080
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
{"Flip":"Off","Mirror":"Off","Effect":"Off","WDREnable":"Off","WideDynamicRange":"0","Gamma":"0","BackLightCom":"Off","DeFlicker":"1","Portrait":"Off","Brightness":"50","Color":"50","Hue":"50","HighlightComp":"Off","Contrast":"50","Sharpness":"50","TWODNR":"20","ThreeDNR":"30"}↵
 
concole.log of pm.responce gives me: undefined
 
POST http://192.168.20.150:8080/picsetup

```

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.

Thanks,  
DaveK

---

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [February 28, 2024, 1:06am UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214/2 "2024-02-28T01:06:08Z")

</div>

I FOUND THE ANSWER!!!

It is the use of pm.response.json() which should have been just response.json. I found the information in [this](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#send-an-asynchronous-request) article.

## Send an asynchronous request

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

```auto
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

```

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 😜

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 28, 2024, 12:54pm UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214/3 "2024-02-28T12:54:33Z")

</div>

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](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/)

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

[Send asynchronous requests with Postman’s PM API | Postman Blog](https://blog.postman.com/send-asynchronous-requests-with-postmans-pm-api/)

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.

---

<div class="post-metadata">

**Author:** ![birddog-davek](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/birddog-davek/32/28489_2.png) [@birddog-davek](https://community.postman.com/u/birddog-davek)\
**Post date:** [February 28, 2024, 11:16pm UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214/4 "2024-02-28T23:16:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [March 29, 2024, 11:16pm UTC](https://community.postman.com/t/accessing-get-request-response-body-in-pre-request-script/58214/5 "2024-03-29T23:16:58Z")

</div>

Closed on March 29, 2024, 11:16pm UTC

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