TypeError when calling a endpoint with an async request

I’m have a script that calls an API to get a token. I saved this token in a environment variable and it was working correctly, but I don’t know why I’m getting this error:

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

pm.sendRequest({
  url: pm.environment.get("url") + 'login',
  method: 'POST', 
  header: {
    'content-type': 'application/json'
  },
  body: {
    mode: 'raw'
    raw: JSON.stringfy({
      "username": pm.environment.get("username"),
      "password": pm.environment.get("password")
    })
}
}, function(err, res) {
  pm.environment.set("token", res.json().resultObject.token);
})

Anyone knows what I am doing wrong?

Hey @Minhaco :wave:

Welcome to the Postman Community! :postman:

Can you open the Postman Console to check what is actually getting sent in the request and what’s returned as the response body.

Here’s an example of doing that for a sample endpoint:


  • What’s the value of the url variable? Does that have a trailing /?
  • Is the correct environment selected?
  • Are the correct credentials being sent in the request?

We’ll need to build up the contextual information here to establish what might be happening.

Following on from Danny’s advice.

This part of your script can be extended to hande error situations.

}, function(err, res) {
  pm.environment.set("token", res.json().resultObject.token);
})

Consider the following.

function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.test("Send request status code = 200", () => {
            pm.expect(res).to.have.status(200);
            let resJson = res.json();
            // console.log(resJson);
            pm.environment.set("token", resJson.resultObject.token);
            // console.log(pm.environment.get("token"));
        });
    }
};

This includes a test and some console log placeholders for troubleshooting.

The error you are getting is because the res object is not defined. Therefore the json() element doesn’t exist causing the error.

  • What’s the value of the url variable? Does that have a trailing /?
    R: The value has the start part of the full endpoint without the last ‘/’.
  • Is the correct environment selected?
    R: Yes, I’ve checked.
  • Are the correct credentials being sent in the request?
    R: Yes, because it was working before.

I made the update on my script following the recomendation of Mike Jones and both of you are corrects.

Probably someone altered the credentials to log in and I couldn’t catch this error with the previous code, because it was not prepared to receive a statusCode 204.

Thank you so much!

Just looking at part your initial script again:

pm.sendRequest({
  url: pm.environment.get("url") + 'login',
  method: 'POST', 
  header: {
    'content-type': 'application/json'
  },
  body: {
    mode: 'raw'
    raw: JSON.stringfy({
      "username": pm.environment.get("username"),
      "password": pm.environment.get("password")
    })
}

That would have been throwing an error in 2 places with the stringfy and the missing comma after raw

There’s no reason why that wouldn’t have worked, unless it wasn’t successfully completing the request and returning JSON, like it was returning a HTML error page etc.

pm.environment.set("token", res.json().resultObject.token);

It would be the same as doing this on the main request:

pm.environment.set("token", pm.response.json().something.data);

Although assigning in to a variable first is a better practice.


Adding the test in there is a great practice to be sure that the request is actually doing what’s expected, with the data that’s being sent.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.