Add Redirect::to constructor (#255)

The motivation for this is established in the issue it fixes.

Resolves #248
This commit is contained in:
Jonas Platte 2021-08-24 12:13:18 +02:00 committed by GitHub
parent 1a5f977896
commit 536b8ca4ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- 👀
- **added:** Add `Redirect::to` constructor ([#255](https://github.com/tokio-rs/axum/pull/255))
# 0.2.0 (23. August, 2021)

View File

@ -29,8 +29,27 @@ pub struct Redirect {
}
impl Redirect {
/// Create a new [`Redirect`] that uses a [`303 See Other`][mdn] status code.
///
/// This redirect instructs the client to change the method to GET for the subsequent request
/// to the given `uri`, which is useful after successful form submission, file upload or when
/// you generally don't want the redirected-to page to observe the original request method and
/// body (if non-empty).
///
/// # Panics
///
/// If `uri` isn't a valid [`HeaderValue`].
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
pub fn to(uri: Uri) -> Self {
Self::with_status_code(StatusCode::SEE_OTHER, uri)
}
/// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
///
/// This has the same behavior as [`Redirect::to`], except it will preserve the original HTTP
/// method and body.
///
/// # Panics
///
/// If `uri` isn't a valid [`HeaderValue`].
@ -53,6 +72,11 @@ impl Redirect {
/// Create a new [`Redirect`] that uses a [`302 Found`][mdn] status code.
///
/// This is the same as [`Redirect::temporary`], except the status code is older and thus
/// supported by some legacy applications that doesn't understand the newer one, but some of
/// those applications wrongly apply [`Redirect::to`] (`303 See Other`) semantics for this
/// status code. It should be avoided where possible.
///
/// # Panics
///
/// If `uri` isn't a valid [`HeaderValue`].