Program to get the token

Hi

I am just started on the POSTMAN API.

So far I am able to get the URLs working correctly.

The issue is that a token keeps expiring which makes my testing more tedious. I have used variables and that is saving some work.

I am try this function to get a regular checking of the token value.

pm.sendRequest('https://{{URL}}/token?id={{client_id}}&secret={{client_secret}}', function (err, res) {
    if (err) {
        console.log(err);
    } else {
        getresponse = "token";
        pm.environment.set("M_token",getresponse);
    }
})

I am trying to get the “token” value return from the Get URL but I am not able to

The {{URL}} returns:

{
success : true,
token : longstringoftokenvalue  
} 

Thank you for any assistance.

Tan Eng Siong

Hey @engsiong

Thank you for your question.

In order to access the response data, you need to use the res argument from the pm.sendRequest() function. This will allow you to grab the part of the response body you need and save this as an environment variable, to use in your other requests.

I’ve changed your code slightly to get the token value and set it as a variable.

 pm.sendRequest('https://{{URL}}/token?id={{client_id}}&secret={{client_secret}}', function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.environment.set("M_token", res.json().token);
    }
})

I wrote a post a while ago about how to make this action of getting the token slightly more automated - Take a look at the code in the post, hopefully, it makes sense.

Hi Danny

I am sure your steps are correct but I still cannot get the thing to work.

Let me chew on this over a few days.

Appreciate your assistance on this matter.

No worries, happy to help.

If you wanted to post some images and show me what you have at the moment (mask any sensitive information), I can hopefully try and see what the issue is.

Hi Danny

Crack the issue. There were 2 problems.

  1. I had use the incorrect URL. There were 2 URLs but I used the same URL variable.
  2. There was also a syntax issue with the variable declaration.

But thanks for at least letting me know that the code was correct and to spend the time looking elsewhere.

1 Like