1
votes

I have a very simple API response, for a POST Request. (Basically this response is a JWT Token)

"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJJbnZlbnRvcnlTZXJ2aWNlQWNjZXNzVG9r........."

enter image description here

I am trying to add this as an Authorization token in Environment variable. But its throwing me an error.

Tests code i have written is var response = JSON.parse(responseBody); pm.environment.set("Authorization", response);

I am getting following error for the same.

"Unexpected token 'e' at 1:1 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJJbnZlbnRvcnlTZXJ2aWNlQWNjZXNzVG"

2

2 Answers

2
votes

This response isn't JSON and that's why you're seeing the error when you try and parse it. You would need to capture this response as plain text instead, which is simple as you want the whole payload:

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

That should grab that response and save it as an environment variable to use in other requests with the {{auth_token}} syntax.

If you wanted to store that response value in a different scope, just replace environment with either globals or collectionVariables.

1
votes

try

pm.environment.set("token", pm.response.text());