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