thunder-client-support
thunder-client-support copied to clipboard
JavaScript scripting pre-request feature
Is your feature request related to a problem? Please describe.
Describe the solution you'd like
Describe alternatives you've considered
Implementation:
Hi @anwarulislam can you post an example/use case what you will be doing with pre-request scripting?
Hi @anwarulislam can you post an example/use case what you will be doing with pre-request scripting?
Will be usefull for something like that.
Examples:
- https://liftcodeplay.com/2018/03/18/how-to-automatically-set-a-bearer-token-for-your-postman-requests/
- https://gist.github.com/bcnzer/073f0fc0b959928b0ca2b173230c0669
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.
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 @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. 👍
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:
(obviously you'd need a better syntax which isn't likely to conflict with typical header values. maybe {{hmac({{envVar}})}}
or {{hmac(my-secret)}}
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()
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.
+1 to add pre-request scripting
+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.
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)
Hi @senocak We have system variables, is that useful?
https://github.com/rangav/thunder-client-support#variables
Hello @rangav
That looks pretty hepfull, I will give it a try. Well done. Thanks a lot.
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.
+1 to add pre-request scripting.
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.


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
related #278
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.
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.
Awesome, look forward for this feature!
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.
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 can you provide more details of your use case?
may be existing functionality already can do what you are looking for
@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.
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 -
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.
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.
We could inject javascript code as we do in Postman?
Good work @rangav ! Kind regards.
Any timeline on this?
We need to generate a random title parameter for a request otherwise the validation fails when testing the feature.
Cool, something like this will be very useful in my current workfllow
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.
Good News, This feature development is completed now. Will publish new release later today
Here is the updated screenshot.
related to #249