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

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):

url: “URL”,
method: “POST”,
header: {
“Content-Type”: “application/x-www-form-urlencoded”
body: {
mode: “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();

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’)

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

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

(error, response)

Which is then accessible by…

var response = response.json()

Not pm.response.

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

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

  if (error) {

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


1 Like

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.

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

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:

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):



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!

Glad it’s working now.

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