giyu
giyu2w ago

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
Hugo
Hugo2w ago
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.
giyu
giyuOP2w ago
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
Hugo
Hugo2w ago
but if you were able to send a location header, that would work right?
giyu
giyuOP2w ago
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...' } }
Hugo
Hugo2w ago
like i said, we plan on adding it, so that should solve your problem
giyu
giyuOP2w ago
ok thanks for clarifying
Hugo
Hugo2w ago
in the meantime you can make it work with html/JS like so (https://www.windmill.dev/docs/core_concepts/webhooks#custom-content-type):
export async function main(
) {
return {
"windmill_content_type": "text/html",
"result": "<h1>Hello</h1>"
}
}
export async function main(
) {
return {
"windmill_content_type": "text/html",
"result": "<h1>Hello</h1>"
}
}
you can also upload an html file to S3 and direct a custom http route to it
giyu
giyuOP2w ago
this is perfect @Hugo C. thanks! this script below correctly redirects to google.com:
export async function main() {
return {
"windmill_content_type": "text/html",
"result": `
<html>
<head>
<meta http-equiv="refresh" content="0; url=https://www.google.com">
</head>
<body>
Redirecting to Google...
</body>
</html>
`
}
}
export async function main() {
return {
"windmill_content_type": "text/html",
"result": `
<html>
<head>
<meta http-equiv="refresh" content="0; url=https://www.google.com">
</head>
<body>
Redirecting to Google...
</body>
</html>
`
}
}