Unable to capture the response and set it as variable

Hello All,

Greetings!!.

I am trying to correlate the response of a GET request & set it as an environment variable. Unfortunately I am not able to capture the response & getting “There was an error in evaluating the test script: JSONError: Unexpected token ‘e’ at 1:1” error.

Please find more details below.

GET method
Content-Type:application/json

ref screenshots:


I had tried the below snippets.
1)
var jsonData = JSON.parse(responseBody);
pm.environment.set(“auth_token”, jsonData.json().val);
console.log(“auth_token created”);

var auth_token= JSON.parse(responseBody);
pm.environment.set(“auth”,auth_token);

var jsonData = JSON.parse(responseBody);
pm.environment.set(“auth_token”, jsonData.responseBody);.

Any suggestions would be helpful.

Thanks in Advance.

Hi there,

The reason that this is erroring is because you are trying to read the response using JSON.parse but the response is not in JSON format. (It complains about the token ‘e’, because this is the first character in the response, when it was expecting to find a { to indicate the start of some JSON.)

If you want to capture the entire response body as your auth token, you should be able to achieve it like this:

pm.environment.set("auth_token", pm.response.text())

1 Like

@neilstudd Thanks for the reply, It worked.

1 Like