Skip to content

add response.redirect() helper#343

Merged
nggit merged 3 commits intomainfrom
redirect
Nov 4, 2025
Merged

add response.redirect() helper#343
nggit merged 3 commits intomainfrom
redirect

Conversation

@nggit
Copy link
Copy Markdown
Owner

@nggit nggit commented Nov 4, 2025

Redirection is basically just to set the header and return an empty body:

@app.route('/example')
async def redirect_handler(response, **server):
    response.set_status(301, 'Moved Permanently')
    response.set_header('Location', 'http://example.com/')
    return ''

Now we can use a helper:

@app.route('/redirect')
async def redirect_helper_handler(response, **server):
    raise response.redirect('http://example.com/', code=301)

It's an instance of tremolo.exceptions.HTTPRedirect.

Note

  • It's the only exception that is not considered an error. So it cannot be hooked with @app.error(301)
  • Just like other exceptions, previous headers are also cleared, meaning you cannot Set-Cookie. Unlike the return '' way

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Nov 4, 2025

@nggit nggit merged commit 0519f84 into main Nov 4, 2025
25 checks passed
@nggit nggit deleted the redirect branch November 4, 2025 13:28
@nggit nggit mentioned this pull request Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant