1

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> ^

or

Unexpected '<' 

or

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.

closed as off-topic by Michael Durrant, NarendraC, IAmMilinPatel, c32hedge, Nitin Rastogi Apr 2 at 4:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development unrelated to testing are off-topic here, but can be asked on Stack Overflow." – Michael Durrant, NarendraC, IAmMilinPatel, c32hedge, Nitin Rastogi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 at 10:07
0

Try this in postman:

var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("token1",(jsonData.results[0].token));

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/

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