thunder-client-support icon indicating copy to clipboard operation
thunder-client-support copied to clipboard

Enable basic additional operations with setTo

Open chrisnurse opened this issue 3 years ago • 8 comments

Description

When executing tests and using setTo it would useful to be able to extract values using basic commands, something that we can do with Postman scripting. However, I don't feel we need an entire scripting capability in order to elevate the power of the setTo option.

We can use a simple expression evaluator to add very valuable features.

Example use case

After we call a create user API method (for example in keycloak), it returns a Location header: http://devhost:8093/auth/admin/realms/app/users/13f239e7-8771-4e75-9c88-341f62650f1b

Example syntax

header.location.split("/")[8] // Split the URL into segments and take the approriate element

This would make the value 13f239e7-8771-4e75-9c88-341f62650f1b available as an environment variable.

After creating a test user it is then possible to delete the same user and clean up.

Postman equivalent

let location = pm.response.headers.get('location').split('/');
let user_id = location[8];
postman.setEnvironmentVariable("kc_new_user_id", user_id);

chrisnurse avatar Feb 26 '22 03:02 chrisnurse

Thanks @chrisnurse for the feedback, Will add this to roadmap

rangav avatar Feb 26 '22 03:02 rangav

You're very welcome.

Thank you for this fantastic contribution to the community. I am loving Thunder Client!

I can recommend https://github.com/SujiJoe/ts-expression-evaluator

This has helped me to build several features that need expression evaluation, such as dynamic forms, where some fields need to appear or be invisible based on some flexible logic and values of other input fields. I found it very intuitive.

chrisnurse avatar Feb 26 '22 12:02 chrisnurse

@chrisnurse glad to know you like the extension 🙂

thanks for suggestion, will look into it.

rangav avatar Feb 26 '22 14:02 rangav

Hi @rangav , do you have any update on this or any alternate to achieve the same?

namhuynhkien avatar Jun 28 '22 05:06 namhuynhkien

@namhuynhkien what is your use-case, can you please share more details

rangav avatar Jun 28 '22 06:06 rangav

The same as Chris. the response returns the location in the header which contain value needed for next request. e.g. for example I want to get the useCaseId. Below is my script in Postman

var loc= pm.response.headers.get('Location');
pm.environment.set("location", loc);
var firstindex = loc.lastIndexOf('=') + 1;
var requestId = loc.substring(firstindex);
pm.environment.set("use_case_id", useCaseId);

namhuynhkien avatar Jun 28 '22 08:06 namhuynhkien

@namhuynhkien thanks for sharing, will implement a solution asap.

rangav avatar Jun 28 '22 11:06 rangav

Another useful thing would be to be able to base64 decode for example a jwt, and then use one of the json properties inside to set an env variable.

skela avatar Jul 05 '22 22:07 skela

This feature is now implemented and published to marketplace, please update to v2.0.0

See all features released https://github.com/rangav/thunder-client-support/releases/tag/v2.0.0

Please let me know your feedback.

rangav avatar Nov 17 '22 12:11 rangav

When setting the env variable in Tests tab you can use the format - header.location | split("/") | slice(7, 8) | join

Will implement at filter in the next update

rangav avatar Nov 17 '22 16:11 rangav

We have implemented at filter in v2.0.1 https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#at

Now you can header.location | split("/") | at(8)

see release notes here https://github.com/rangav/thunder-client-support/releases/tag/v2.0.1

rangav avatar Nov 24 '22 13:11 rangav