Support for HTTP Browser Redirects in Windmill Functions (302 Response Handling)
Hey guys,
I'm trying to implement URL redirects through a Windmill TypeScript function.
Specifically, I want to create an endpoint that, when accessed through a browser, redirects the user to another URL (in my test case, to Google.com).
Current behavior:
When I create a TypeScript function that returns redirect information like:
typescriptCopyexport async function main() {
return {
status: 302,
headers: {
'Location': 'https://www.google.com'
}
};
}
And access it via GET request (using the sync -> GET by path method), it returns the JSON response:
jsonCopy{"status": 302, "headers": {"Location": "https://www.google.com"}}
instead of actually redirecting the browser.
I've also tried:
- Returning HTML content with meta refresh or JavaScript redirect
- Using data:URL approach with base64 encoded HTML
- Setting different Content-Type headers
However, all attempts result in the JSON being displayed rather than the browser being redirected.
My question: Is it possible to configure Windmill functions to handle proper HTTP redirects (302 status codes) when accessed directly via browser, rather than just returning JSON? Or is there a recommended pattern for implementing URL redirects in Windmill?
This functionality would be particularly useful for implementing features like:
- Email click tracking with redirects
- OAuth callback handlers
- Short URL services
Any scenario where users need to be redirected through a Windmill endpoint
Technical goal: When a user clicks a link to my Windmill endpoint, their browser should automatically redirect to the destination URL, rather than displaying the JSON response.
8 Replies
to send a specific status you can use this: https://www.windmill.dev/docs/script_editor/custom_response_code
it's not yet possible to send headers but it will be added soon
Custom response code | Windmill
For all sync run jobs endpoints, if the response contains a key windmillstatuscode with a number value, that value will be used as the status code.
I mean my intention is to call a windmill function via GET URL and then some custom code runs on the backend and then it redirects the client browser to "google.com" - the video below explains it
but if you were able to send a location header, that would work right?
how would I do that in the script? I've tried this but it doesnt work
import { Response } from '@windmill/worker-typescript'
export async function main(): Promise<Response<any>> {
return {
statusCode: 302,
headers: {
'Location': 'https://www.google.com'
},
response: 'Redirecting to Google...'
}
}
like i said, we plan on adding it, so that should solve your problem
ok thanks for clarifying
in the meantime you can make it work with html/JS like so (https://www.windmill.dev/docs/core_concepts/webhooks#custom-content-type):
you can also upload an html file to S3 and direct a custom http route to it
this is perfect @Hugo C. thanks! this script below correctly redirects to google.com: