Pm.response.json() returns response undefined, but valid json response is returned with 200 status

This is my ā€˜Testā€™ script

 var resp = pm.response.json();
 var token = resp.access_token;
 pm.globals.set("service_token", token);

Using the desktop app, version 6.6.1

Iā€™m making a Type ā€œNo Authā€ POST call for an access token. The call returns 200 status with a valid JSON object,

{
    "access_token": "jwt_token_value_redacted_for_this_example",
    "scope": "read:client_grants create:client_grants delete:client_grants update:client_grants read:users update:users delete:users create:users read:users_app_metadata update:users_app_metadata delete:users_app_metadata",
    "expires_in": 3600,
    "token_type": "Bearer"
}

In ā€˜Test Resultsā€™ I get ā€œThere are no tests for this requestā€

In Postman console when I run resp = pm.response.json(); I get ā€œCannot read property ā€˜jsonā€™ of undefinedā€.

pm.responseBody and pm.response return ā€˜undefinedā€™

I donā€™t seem to be able to get at the response body at all. Any idea why, please?

Ultimately, I wish to store the access token from the response in a global variable that I can then {{use}} in various API calls.
Thanks

1 Like

What do you see in the Postman console for console.log(pm.response.json().access_token)?

Is that the value that you want?

Would you be about to share a screen shot of what you seeing to add context?

The snippet you posted did work. Turns out the issue not that itā€™s not working. The token is being captured and saved to a global variable.

The issue is I interpreted the verbiage ā€œThere are no tests for this requestā€ in the ā€˜Tests Resultsā€™ tab to mean that the script didnā€™t run.

I also interpreted the Uncaught TypeError: Cannot read property 'json' of undefined at <anonymous>:1:24 message to mean that pm.response.json() was returning empty or null.

Thanks so much for your help.

1 Like