# SendRequest in a Pre-request Script on Collection - how to get response to set Collection Variable

**URL:** <https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [October 19, 2022, 9:57am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211 "2022-10-19T09:57:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacreator](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dacreator](https://community.postman.com/u/dacreator)\
**Post date:** [October 19, 2022, 9:57am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/1 "2022-10-19T09:57:25Z")

</div>

_My question_:  
My goal is to trigger a POST request for TokenRefresh and get the RefreshedToken Info from the request response and store it in a Collection variable.

Each request should then be able to use the updated Collection variable for its authentification.

_Details (like screenshots)_:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/f/afffa2c93d5a598753792898e75a816844f908f3.png)

pm.sendRequest({  
url: “URL”,  
method: “POST”,  
header: {  
“Content-Type”: “application/x-www-form-urlencoded”  
},  
body: {  
mode: “urlencoded”,  
urlencoded:  
[  
{key: “client\_id”,value: “XXX”},  
{key: “grant\_type”,value: “refresh\_token”},  
{key: “refresh\_token”,value: “ACCESSTOKEN”},  
]  
}

});

//Now I want to search the response for access\_token and save it in the Collection variable

let jsonResponse = pm.response.json();  
console.log(jsonResponse);

pm.collectionVariables.set(“CollRefToken”, jsonResponse.access\_token)

_How I found the problem_:

When I send a request in the Collection, the Auth POST request is correctly sent, but something is wrong in my setting the Collection variable:

“TypeError: Cannot read properties of undefined (reading ‘json’)”  
There was an error in evaluating the Pre-request Script:Error: Cannot read properties of undefined (reading ‘json’)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/0/5027006fbbad54bf4abafb418523e2f371ad96ec.png)

---

<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:** [October 19, 2022, 11:27am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/2 "2022-10-19T11:27:22Z")

</div>

Looks like you are missing a key element in the sendRequest function which is the variable for handling the response.

Therefore, its erroring on the line pm.response.json(); as pm.response will be blank, so the json() function is failing.

Have a look at the following examples…

[Postman JavaScript reference | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts)

The bit you are missing is the following which defines where the response is stored.

```auto
(error, response)

```

Which is then accessible by…

```auto
var response = response.json()

```

Not pm.response.

---

<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:** [October 19, 2022, 11:37am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/3 "2022-10-19T11:37:03Z")

</div>

```auto
const postRequest = {
  url: 'https://url',
  method: 'POST',
  header: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded:
    [
        {key: "client_id",value: "XXX"},
        {key: "grant_type",value: "refresh_token"},
        {key: "refresh_token",value: "ACCESSTOKEN"},
    ]
  }
};

pm.sendRequest(postRequest, (error, response) => {

  if (error) {
    console.log(error);
  }

  pm.test('response should be okay to process', () => {
    pm.expect(error).to.equal(null);
    pm.expect(response).to.have.property('code', 200);
    pm.expect(response).to.have.property('status', 'OK');
    pm.collectionVariables.set("CollRefToken", response.json().access_token);

  });

});

```

---

<div class="post-metadata">

**Author:** ![dacreator](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dacreator](https://community.postman.com/u/dacreator)\
**Post date:** [October 21, 2022, 7:36am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/4 "2022-10-21T07:36:03Z")

</div>

Hi MDJones,

thanks for your detailed help!  
I pasted your code into the Collection Pre-Script and updated my parameters and saved the changes.  
When I POST a request in the collection, no error is generated, status 200.  
But I can see that the Collection Variable CollRefToken is not being updated.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/5/0563eaf1c8e7cb1eea3a4bce5c997388492e3912.png)

What is strange that in the request, the variable value of CollRefTokenDev (Refreshed Token should be used as Auth), still has an old value.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/b/7b12c9b002d7a5ccfbf86bb7d99773f0b486dee2.png)

Does the Cookie play a role here? Generally, I want to use the refreshed Token in the Collection Variable CollRefTokenDev for every request, and not some value stored in a cookie.

CURL of request:

```auto
curl --location --request POST 'URL' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGcXXXXXX' \
--header 'Cookie: 5cfd47a23da9b9e1c98a84d0986d03c4=bb651d69ae861b858c8e7b91f7b51002' \
--data-raw '{
"$type": "BLA",
"Message": "Some Message expected to be received and processed by the system",
"MessageType": "BLA"
}'

```

I think this is related to my other post, where there is a Collection using new environment with Cookie (no value update) in comparison with a Collection using an old environment without cookie (value is being updated):

> [@Setting Collection Variable doesn't work](https://community.postman.com/t/setting-collection-variable-doesnt-work/40305):
>
> My question: I have 2 collections. In C1 I send a request to get the response value of “access\_type” and set its value in the Collection variable “CollRefToken”. This works fine. every time I POST the request, the current value of CollRefToken is being updated. The Code to set the CollVar is under “Tests” in the POST request. In C2 I want to do the same. Same code, but here the Collection variable is not being updated. The request runs without error in both collections. Details (like screen…

Thanks

---

<div class="post-metadata">

**Author:** ![dacreator](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dacreator](https://community.postman.com/u/dacreator)\
**Post date:** [October 21, 2022, 9:11am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/5 "2022-10-21T09:11:57Z")

</div>

Hi,

I tinkered around a little and now it works.  
Can’t identify the root cause, I assume some spelling mistake on my side.

Thanks for helping!

---

<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:** [October 21, 2022, 10:00am UTC](https://community.postman.com/t/sendrequest-in-a-pre-request-script-on-collection-how-to-get-response-to-set-collection-variable/40211/6 "2022-10-21T10:00:40Z")

</div>

Glad it’s working now.

I find that cut and paste screws up the quotes a lot of the times.
