thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.61k stars 126 forks source link

Authorization tokens not recognized #1496

Closed esp0314 closed 5 months ago

esp0314 commented 5 months ago

@rangav, After this bug is fixed(https://github.com/rangav/thunder-client-support/issues/1488), we have updated the thunderclient version to v2.19.0 and cli v.1.12.1, but we encountered another issue.

Our api endpoint is requiring Authorization, and before this endpoint is executed, we must retrieve first the authorization tokens. In our setup, we have a pre-run api that returns tokens and saved in local environment and will be used as authorization headers in our main endpoints. Looks like the authorization is not recognized during the initial execution and it is giving 401 Unauthorized response. And it is only recognized when the endpoint is executed the 2nd time around. Please help check

Pre-run endpoint image

Token saved in the local environment image

Main endpoint image

2nd attempt image

We have to revert to v2.18.0 and cli 1.12.0 and it is working fine for the authorization token headers, but we have to deal with the file not found issue again(https://github.com/rangav/thunder-client-support/issues/1488)

rangav commented 5 months ago

Thanks @esp0314 for reporting the bug, Will verify and get back to you asap.

rangav commented 5 months ago

Can you please verify the logs in Output window after the first attempt and see what headers are sent?

You can verify the access token is sent or not in 1st attempt

also you can compare the logs with 2nd attempt request

esp0314 commented 5 months ago

1st attempt image

2nd attempt image

rangav commented 5 months ago

Thanks for sharing the logs.

how you are saving the access token from response to Local Env?

Will try to re-produce the issue here locally

esp0314 commented 5 months ago

Basically, we have a a main request that is requiring authorization token, and this main request has a pre custom script that will run another request that returns the access token and used "setTo" to save the token in local environment. image

image

rangav commented 5 months ago

Little confused, want to clarify more

The screenshot above is it main request? And where is the above script running?

esp0314 commented 5 months ago

Sorry for the confusion. I have edited the previous reply

rangav commented 5 months ago

I think you are using it wrong.

You are setting the access_token after the main request is executed in Tests, that's why the main request header is not getting the access_token value.

You should set the access token directly in the pre-req script.


var response = await tc.runRequest("reqId");
tc.setVar("access_token", response.json.access_token, "local");
esp0314 commented 5 months ago

Hi @rangav, I see your point, your suggestion is the same implementation as mine. The only difference is that, saving the token is in the pre-req script, while mine is i saved it via UI. We have been implementing this kind of setup since we started using thunderclient as our primary tool in api testing. As i have mentioned in the previous thread, it is working on the previous version.

Here's the setup:

  1. Collections settings image

image

image

  1. In the main request, it uses this pre request filter(environmentEndPoint)
  2. Pre-request filter will also call another request that returns the access token and save it to local environment.

image

rangav commented 5 months ago

Thanks, now I understand how its setup. Its a correct setup, no need to change anything.

i will try to re-produce the issue and get back to you asap.

esp0314 commented 5 months ago

Thank you!!

rangav commented 5 months ago

We identified the bug and will publish a fix later today. Thanks for reporting.

rangav commented 5 months ago

This bug is fixed and published to the marketplace. Please update to v2.19.4.

You can see all the features released in this update https://github.com/rangav/thunder-client-support/releases/tag/v2.19.4

Please test it and let me know your feedback

esp0314 commented 5 months ago

Hi @rangav,

Verified to be fixed. Thank you

rangav commented 5 months ago

Thanks for confirmation