0

I am trying to write a Postman pre-request script that will set my env variable for the token. I've taken one of my other working scripts and modified it for this type of response but am having issues.

I have a web service that returns the Auth Token back in the request as follows

enter image description here

I have a script that should parse the new token but it throws an error

    pm.sendRequest({
    url: pm.environment.get("token_endpoint")+ '?username=' + pm.environment.get("username") + '&password='+ pm.environment.get("password"),
    method: 'GET',
  }, 

function (err, res) {
      if (err) {
          throw err;
      }
      if (res.code !== 200) {
          throw new Error('Could not log in.');
      }
     console.log(`New token is: ${res.json()}`);
              
});

my error trying to log the token is

JSONError: Unexpected token 'e' at 1:1 eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiU2VydmljZURpcmVjdG9yQVBJIiwibm

I've tried a few different ways to pull that in but nothing logs the token as you would expect. What am I doing wrong here?

Tim
  • 1,249
  • 5
  • 28
  • 54
  • 2
    Well, the result of the request is not json but only a JWT, so you can't parse it to json (what `res.json()` tries to do). Try `res.text()` instead – derpirscher Aug 02 '21 at 20:02
  • @derpirscher, thanks I could have sworn I tried that. Maybe it was the way I was trying to log it. but that worked. Thanks so much. – Tim Aug 02 '21 at 22:07

0 Answers0