I am trying to chain API request on using POSTMAN. I am writing a script in the test section for extracting token from the response body and storing it in an environment variable. This is my code in the test section:

var jsonObject = xml2Json(responseBody);
let firstresponse= pm.response.json().results[0];
let token = firstresponse.token;
pm.environment.set("token1", token);

However, when i run the request, i am getting multiple errors like

JSONError: Unexpected token '<' at 1:1 <!DOCTYPE html> ^


Unexpected '<' 


Unexpected token u

I tried googling it, and many have experience similar problems, but none the solution helped me. Is it a bug in Postman or is their any workaround? Anybody has any solutions. I am using POSTMAN Version 6.2.0-canary01.

  • Which line produces the error message?
    – Alexey R.
    Jun 4 '18 at 11:12
  • Looks like your response is not a JSON string but HTML. Could be you get an error page (f.i. http error). What is your complete respons?
    – Patrick
    Jun 4 '18 at 13:45
  • @Patrick I am getting the proper response of the login page,but in HTML. It can be converted to XML by selecting the option provided there. But when I chose JSON in that list, Unexpected '<' appears.
    – QA9
    Jun 5 '18 at 4:16
  • @AlexeyR. I am not sure which line produces the error. Because it just gives Unexpected '<' in response body in JSON format
    – QA9
    Jun 5 '18 at 4:16
  • 1
    I eventually figured a workaround, but any help is appreciated for future @DannyDainton
    – QA9
    Jan 25 '19 at 10:07

Try this in postman:

var jsonData = JSON.parse(responseBody);

This is an example where you get the token from your JSON reponse and set it in a Global Variable. You can just change it to environment also if you like.

More info about test scripts in postman: https://learning.getpostman.com/docs/postman/scripts/test_examples/

  • I tried this suggestion - and got two or three errors instead of just one.
    – Henke
    Jan 9 at 12:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.