Struct rocket::http::Status[][src]

pub struct Status {
    pub code: u16,
}
Expand description

Structure representing an HTTP status: an integer code.

A Status should rarely be created directly. Instead, an associated constant should be used; one is declared for every status defined in the HTTP standard. If a custom status code must be created, note that it is not possible to set a custom reason phrase.

use rocket::http::Status;

// Create a status from a known constant.
let ok = Status::Ok;
assert_eq!(ok.code, 200);
assert_eq!(ok.reason(), Some("OK"));

let not_found = Status::NotFound;
assert_eq!(not_found.code, 404);
assert_eq!(not_found.reason(), Some("Not Found"));

// Or from a status code: `reason()` returns the phrase when known.
let gone = Status::new(410);
assert_eq!(gone.code, 410);
assert_eq!(gone.reason(), Some("Gone"));

// `reason()` returns `None` when unknown.
let custom = Status::new(599);
assert_eq!(custom.code, 599);
assert_eq!(custom.reason(), None);

Responding

To set a custom Status on a response, use a response::status responder, which enforces correct status-based responses. Alternatively, respond with (Status, T) where T: Responder, but beware that the response may be invalid if it requires additional headers.

use rocket::http::Status;

#[get("/")]
fn index() -> (Status, &'static str) {
    (Status::NotFound, "Hey, there's no index!")
}

Fields

code: u16

The HTTP status code associated with this status.

Implementations

Status with code 100 .

Status with code 101 .

Status with code 102 .

Status with code 200 .

Status with code 201 .

Status with code 202 .

Status with code 203 .

Status with code 204 .

Status with code 205 .

Status with code 206 .

Status with code 207 .

Status with code 208 .

Status with code 226 .

Status with code 300 .

Status with code 301 .

Status with code 302 .

Status with code 303 .

Status with code 304 .

Status with code 305 .

Status with code 307 .

Status with code 308 .

Status with code 400 .

Status with code 401 .

Status with code 402 .

Status with code 403 .

Status with code 404 .

Status with code 405 .

Status with code 406 .

Status with code 407 .

Status with code 408 .

Status with code 409 .

Status with code 410 .

Status with code 411 .

Status with code 412 .

Status with code 413 .

Status with code 414 .

Status with code 415 .

Status with code 416 .

Status with code 417 .

Status with code 418 .

Status with code 421 .

Status with code 422 .

Status with code 423 .

Status with code 424 .

Status with code 426 .

Status with code 428 .

Status with code 429 .

Status with code 431 .

Status with code 451 .

Status with code 500 .

Status with code 501 .

Status with code 502 .

Status with code 503 .

Status with code 504 .

Status with code 505 .

Status with code 506 .

Status with code 507 .

Status with code 508 .

Status with code 510 .

Status with code 511 .

Creates a new Status with code. This should be used only to construct non-standard HTTP statuses. Use an associated constant for standard statuses.

Example

Create a custom 299 status:

use rocket::http::Status;

let custom = Status::new(299);
assert_eq!(custom.code, 299);

Returns the class of a given status.

Example

use rocket::http::{Status, StatusClass};

let processing = Status::Processing;
assert_eq!(processing.class(), StatusClass::Informational);

let ok = Status::Ok;
assert_eq!(ok.class(), StatusClass::Success);

let see_other = Status::SeeOther;
assert_eq!(see_other.class(), StatusClass::Redirection);

let not_found = Status::NotFound;
assert_eq!(not_found.class(), StatusClass::ClientError);

let internal_error = Status::InternalServerError;
assert_eq!(internal_error.class(), StatusClass::ServerError);

let custom = Status::new(600);
assert_eq!(custom.class(), StatusClass::Unknown);

Returns a Status given a standard status code code. If code is not a known status code, None is returned.

Example

Create a Status from a known code:

use rocket::http::Status;

let not_found = Status::from_code(404);
assert_eq!(not_found, Some(Status::NotFound));

Create a Status from an unknown code:

use rocket::http::Status;

let unknown = Status::from_code(600);
assert!(unknown.is_none());

Returns the canonical reason phrase if self corresponds to a canonical, known status code. Otherwise, returns None.

Example

Reason phrase from a known code:

use rocket::http::Status;

assert_eq!(Status::Created.reason(), Some("Created"));
assert_eq!(Status::new(200).reason(), Some("OK"));

Absent phrase from an unknown code:

use rocket::http::Status;

assert_eq!(Status::new(499).reason(), None);

Returns the canonical reason phrase if self corresponds to a canonical, known status code, or an unspecified but relevant reason phrase otherwise.

Example

use rocket::http::Status;

assert_eq!(Status::NotFound.reason_lossy(), "Not Found");
assert_eq!(Status::new(100).reason_lossy(), "Continue");
assert!(!Status::new(699).reason_lossy().is_empty());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The response generated by Status depends on the status code itself. The table below summarizes the functionality:

Status Code RangeResponse
[400, 599]Forwards to catcher for given status.
100, [200, 205]Empty with status of self.
All others.Invalid. Errors to 500 catcher.

In short, a client or server error status codes will forward to the corresponding error catcher, a successful status code less than 206 or 100 responds with any empty body and the given status code, and all other status code emit an error message and forward to the 500 (internal server error) catcher.

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.