Struct rocket::response::Redirect[][src]

pub struct Redirect(_, _);
Expand description

An empty redirect response to a given URL.

This type simplifies returning a redirect response to the client.

Usage

All constructors accept a generic type of T: TryInto<Reference<'static>>. Among the candidate types are:

Any non-'static strings must first be allocated using .to_string() or similar before being passed to a Redirect constructor. When redirecting to a route, or any URI containing a route, always use uri! to construct a valid URI:

use rocket::response::Redirect;

#[get("/hello/<name>/<age>")]
fn hello(name: String, age: u8) -> String {
    format!("Hello, {} year old named {}!", age, name)
}

#[get("/hi/<name>/<age>")]
fn hi(name: String, age: u8) -> Redirect {
    Redirect::to(uri!(hello(name, age)))
}

#[get("/bye/<name>/<age>")]
fn bye(name: String, age: u8) -> Redirect {
    Redirect::to(uri!("https://rocket.rs/bye", hello(name, age), "?bye#now"))
}

Implementations

impl Redirect[src]

pub fn to<U: TryInto<Reference<'static>>>(uri: U) -> Redirect[src]

Construct a temporary “see other” (303) redirect response. This is the typical response when redirecting a user to another page. This type of redirect indicates that the client should look elsewhere, but always via a GET request, for a given resource.

Examples

use rocket::response::Redirect;

let redirect = Redirect::to(uri!("/foo/bar"));
let redirect = Redirect::to(uri!("https://domain.com#foo"));

pub fn temporary<U: TryInto<Reference<'static>>>(uri: U) -> Redirect[src]

Construct a “temporary” (307) redirect response. This response instructs the client to reissue the current request to a different URL, maintaining the contents of the request identically. This means that, for example, a POST request will be resent, contents included, to the requested URL.

Examples

use rocket::response::Redirect;

let redirect = Redirect::temporary(uri!("some/other/path"));
let redirect = Redirect::temporary(uri!("https://rocket.rs?foo"));
let redirect = Redirect::temporary(format!("some-{}-thing", "crazy"));

pub fn permanent<U: TryInto<Reference<'static>>>(uri: U) -> Redirect[src]

Construct a “permanent” (308) redirect response. This redirect must only be used for permanent redirects as it is cached by clients. This response instructs the client to reissue requests for the current URL to a different URL, now and in the future, maintaining the contents of the request identically. This means that, for example, a POST request will be resent, contents included, to the requested URL.

Examples

use rocket::response::Redirect;

let redirect = Redirect::permanent(uri!("/other_url"));
let redirect = Redirect::permanent(format!("some-{}-thing", "crazy"));

pub fn found<U: TryInto<Reference<'static>>>(uri: U) -> Redirect[src]

Construct a temporary “found” (302) redirect response. This response instructs the client to reissue the current request to a different URL, ideally maintaining the contents of the request identically. Unfortunately, different clients may respond differently to this type of redirect, so 303 or 307 redirects, which disambiguate, are preferred.

Examples

use rocket::response::Redirect;

let redirect = Redirect::found(uri!("/other_url"));
let redirect = Redirect::found(format!("some-{}-thing", "crazy"));

pub fn moved<U: TryInto<Reference<'static>>>(uri: U) -> Redirect[src]

Construct a permanent “moved” (301) redirect response. This response should only be used for permanent redirects as it can be cached by browsers. Because different clients may respond differently to this type of redirect, a 308 redirect, which disambiguates, is preferred.

Examples

use rocket::response::Redirect;

let redirect = Redirect::moved(uri!("here"));
let redirect = Redirect::moved(format!("some-{}-thing", "crazy"));

Trait Implementations

impl Debug for Redirect[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<'r> Responder<'r, 'static> for Redirect[src]

Constructs a response with the appropriate status code and the given URL in the Location header field. The body of the response is empty. If the URI value used to create the Responder is an invalid URI, an error of Status::InternalServerError is returned.

fn respond_to(self, _: &'r Request<'_>) -> Result<'static>[src]

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

Auto Trait Implementations

impl !RefUnwindSafe for Redirect

impl Send for Redirect

impl Sync for Redirect

impl Unpin for Redirect

impl UnwindSafe for Redirect

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

pub fn into_collection<A>(self) -> SmallVec<A> where
    A: Array<Item = T>, 

Converts self into a collection.

pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
    F: FnMut(T) -> U,
    A: Array<Item = U>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V