Error on setting variables ( Cannot read properties of undefined )

Hi, I’m trying to set my env to store jwt token, and no matter what I tried I can’t see it working, no value in Env section. I typed this code in `cripts` section, tried for local/global:

const response = pm.response.json();
const jwt =responseJson.jwt;
pm.globals.set(‘jwtGLobal’,‘Bearer ${jwt}’);
pm.variables.set(‘jwtLocal’,‘Bearer ${jwt}’);```

Console message says:
TypeError: Cannot read properties of undefined (reading ‘json’)

What I’m doing wrong, this setup was working 3-4 months ago for me.

Thanks

Vic

Hey @dainova17

It looks like you have a mismatch with your variable names:

const response = pm.responese.json();
const jwt =responseJson.jwt;

You’d need to remove the extra Json part.

Thanks Danny,
Let me check, in your reply you still have extra json part, right ? It’s look identical, less typo in responEse

Something is weird in my setup, no matter what command I ran I get same error ,even for
`console.log("Set authToken variable “)`. And I do SAVE .

I just copied your code and left that in there so I could explain where it was in the script. :folded_hands:t3:

Do you have any other scripts in the Collection level or Request level that’s trying to access the JSON.

As I can’t see what you have in front of you it’s tough for me to point you in a specific direction.

Thanks much ,Danny. Let me do my homework. I will try to do brand new setup.

1 Like

You’re welcome.

It might also be worth checking the pre-request scripts too.

It would likely show the same error message if you’re trying to grab the response, before the main request is sent.

Made brand new env, and have the same result, in this case Script section is empty for both Pre and Post-Response.Post works fine, status =200, see jwt generated. Maybe I can just ignore this message on console?

What is the content type header for the response?

In the image, did you manually change that to Raw or was it returned like that?

It looks like it’s returning that as a string, that looks like JSON. It might be a request/response header missing somewhere though. It would be throwing that error because it’s trying to parse that string as JSON in the pm.response.json() method.

You can use pm.response.text() but you would then need to add some additional logic to extract the value you need. I would personally look closer at the headers first.

Hi Danny,
Here it is : application/x-www-form-urlencoded

I might changed output to RAW to troubleshoot in first pic, here is what I have by default = JSON, it’s look OK for both. I’m sure it’s Json, b’z it worked OK with variable a month ago.

Could be be in some other call inside of my collections? They all GETs. And I’m running only one PUT.
THanks so much

V

Can you remove the manually added request content-type header, so that the auto generated one will be applied.

I didn’t enter it manually, was there probably already with my demo. (application/x-www-form-urlencoded)
Unchecked, and got error on body like below:

What happens when you add the content-type: application/json header?

IT’s same error, original content-type worked fine still. Overall I don’t have any issues with this API running it in python, just trying to do some automation in Postman.
“error”: “invalid_request”,

"error_description": "Invalid Content Type"

Thanks much Danny and all for help.

V