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

JavaScript scripting pre-request feature

Open anwarulislam opened this issue 3 years ago • 27 comments

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

Describe alternatives you've considered

Implementation:

anwarulislam avatar Apr 20 '21 19:04 anwarulislam

Hi @anwarulislam can you post an example/use case what you will be doing with pre-request scripting?

rangav avatar Apr 20 '21 19:04 rangav

Hi @anwarulislam can you post an example/use case what you will be doing with pre-request scripting?

image Will be usefull for something like that.

tiagoCristiano avatar Apr 22 '21 19:04 tiagoCristiano

Examples:

  • https://liftcodeplay.com/2018/03/18/how-to-automatically-set-a-bearer-token-for-your-postman-requests/
  • https://gist.github.com/bcnzer/073f0fc0b959928b0ca2b173230c0669

norlancd avatar Apr 26 '21 03:04 norlancd

This would be very useful. I use pre-request scripts to add tokens to headers of requests in a collection. That way I do not need to manually add them to each request.

S4G4R avatar Apr 26 '21 10:04 S4G4R

Hi @S4G4R @tiagoCristiano @norlancd if the pre-scripts are standard and common use cases, I should be able to build simple UI to hide the complexity of scripting from user - similar to scriptless testing.

Do you guys think that will work? any suggestions

rangav avatar May 02 '21 18:05 rangav

Hi @S4G4R @tiagoCristiano @norlancd if the pre-scripts are standard and common use cases, I should be able to build simple UI to hide the complexity of scripting from user - similar to scriptless testing.

Do you guys think that will work? any suggestions

Don't think it is a bad idea.

For my usecase i need to bulk add headers to all requests inside a collection. I don't mind if it's a UI or manual script. 👍

S4G4R avatar May 03 '21 06:05 S4G4R

Another example is setting a HMAC header which is a hash of a secret key and the request body. Here's an example of someone doing this in postman with a pre-request script: https://torbjornzetterlund.com/how-to-use-hmac-with-postman-to-test-webhooks.

Since the hash is generated from the request body it cannot be set in advance (unless you never change the body).

If you wanted to do this without scripting then all you need is a place for the user to enter the secret and you could calculate the HMAC for them and put it on a particular header.

Eg. in the header UI: image

(obviously you'd need a better syntax which isn't likely to conflict with typical header values. maybe {{hmac({{envVar}})}} or {{hmac(my-secret)}}

wildhart avatar May 14 '21 04:05 wildhart

Hi @S4G4R @tiagoCristiano @norlancd if the pre-scripts are standard and common use cases, I should be able to build simple UI to hide the complexity of scripting from user - similar to scriptless testing.

Do you guys think that will work? any suggestions

Hi @rangav , Its a good idea to have UI to hide complexity but it will be great if developers do have access to modify the script to customize it further.

Common use case in our project is to generate unique payload ( lets say for order processing )

request.body = {
   orderDate: {{#orderDate}},
   productId: {{#productId}}
}

before posting this payload we have a script in place which replaces variables

{{#orderDate}} = someJavaScriptFunction();
{{#productId}} = someJavascriptFunction-That-Invoke-TestAPI-And-Fetch-Random-ProductID()

hemantshelar avatar Jun 01 '21 08:06 hemantshelar

HI @hemantshelar thanks for the suggestions, Will keep a note of these use cases, will provide more details when I start work on this feature.

rangav avatar Jun 01 '21 09:06 rangav

+1 to add pre-request scripting

ahmedelkilani avatar Aug 28 '21 09:08 ahmedelkilani

+1 to add pre-request scripting. use case : often used with building a signature using hmac-md5 algorithm with several variables .eg: const signature = md5(token+timestamp+etc...) and put each vars into headers instead of mix in post-body.

seanchan avatar Sep 08 '21 11:09 seanchan

Hi, this is my postman pre-request script to generate a random text, i am using it for creating a random name. I will use thunder if it has that feature in future let text = ""; const charset = "abcdefghijklmnopqrstuvwxyz0123456789" for( var i=0; i < 8; i++ ) text += charset.charAt(Math.floor(Math.random() * charset.length)) pm.environment.set("random", text)

senocak avatar Oct 08 '21 17:10 senocak

Hi @senocak We have system variables, is that useful?

https://github.com/rangav/thunder-client-support#variables

rangav avatar Oct 08 '21 18:10 rangav

Hello @rangav

That looks pretty hepfull, I will give it a try. Well done. Thanks a lot.

senocak avatar Oct 08 '21 18:10 senocak

My use case is performing real, public client auth code with PKCE OAuth flow to get an Azure AD token for custom OAuth protected Apis. I'm not sure how feasible this is with Thunder Client, as my pre-request script on Postman requires installation and activation of the Postman Interceptor as well, to be able to grab cookies from login.microsoftonline.com.

I've never understood why Postman acts like a confidential client (using a client secret). It is by definition not a confidential client. With my pre-request script and the interceptor, I can run auth the same way my SPA does.

rocknet avatar Nov 11 '21 16:11 rocknet

+1 to add pre-request scripting.

M0H314-Hasani avatar Jan 10 '22 18:01 M0H314-Hasani

JavaScript scripting pre-request function can be usefully used to generate dynamic strings of specific conditions.

  • It is impossible to process only with system variables.
pre1 pre2

blaxk avatar Feb 05 '22 10:02 blaxk

Hi @blaxk thanks for sharing your use-case, I have noted it. Will update this issue when I plan to work on scripting for advanced use-cases

rangav avatar Feb 05 '22 11:02 rangav

related #278

rangav avatar Feb 05 '22 11:02 rangav

Hi All, started work on request linking. here is the preview screenshot of the feature. let me know your feedback

Advanced scripting will be implemented at later stage.

Screenshot 2022-03-08 at 14 39 45

rangav avatar Mar 08 '22 14:03 rangav

Hi All, started work on request linking. here is the preview screenshot of the feature. let me know your feedback

Advanced scripting will be implemented at later stage.

Screenshot 2022-03-08 at 14 39 45

Awesome, look forward for this feature!

skydiveryiyu avatar May 07 '22 19:05 skydiveryiyu

Can we have a way to update the environment variables from the pre-request hook? It's useful if we want to update dynamic content that needs to be stored in the environment variables. One use case could be to automatically update the Bearer Token.

However, thanks for considering to add the pre-request hook.

FarhanShares avatar May 13 '22 06:05 FarhanShares

Really looking for the update for this feature. My use case is simple, in Backend we use firebase token, and I send an authentication request to firebase to get the token.

mirkancal avatar Jul 10 '22 10:07 mirkancal

@mirkancal can you provide more details of your use case?

may be existing functionality already can do what you are looking for

rangav avatar Jul 10 '22 10:07 rangav

@rangav Sure. In Postman I have pre-request script. I use it to get token from firebase to use in my backend requests.

So for this specific collection, before every request, I'm requesting to firebase, get the token, and set it to my header. image

mirkancal avatar Jul 15 '22 11:07 mirkancal

You can achieve this now.

  • Create a first request ( pre-request ) with required details

  • from Tests tab select Set Env Envariable and save the token to variable image

  • In 2nd request the use the token using {{token}}

  • You can add the 2 requests to collection folder and run them together.

let me know if you have further questions.

rangav avatar Jul 15 '22 11:07 rangav

Hi All, started work on request linking. here is the preview screenshot of the feature. let me know your feedback

Advanced scripting will be implemented at later stage.

Screenshot 2022-03-08 at 14 39 45

We could inject javascript code as we do in Postman?

Good work @rangav ! Kind regards.

JavierGonzalezAraujo avatar Sep 21 '22 14:09 JavierGonzalezAraujo

Any timeline on this?

We need to generate a random title parameter for a request otherwise the validation fails when testing the feature.

pupca avatar Sep 27 '22 12:09 pupca

Cool, something like this will be very useful in my current workfllow

piyushchauhan2011 avatar Oct 12 '22 03:10 piyushchauhan2011

Hi All, started work on request linking. here is the preview screenshot of the feature. let me know your feedback

Advanced scripting will be implemented at later stage.

Screenshot 2022-03-08 at 14 39 45

Good News, This feature development is completed now. Will publish new release later today

Here is the updated screenshot. Screenshot 2022-10-12 at 05 16 18 related to #249

rangav avatar Oct 12 '22 04:10 rangav