pub struct StatusCode(_);
Expand description

An HTTP status code (status-code in RFC 7230 et al.).

Constants are provided for known status codes, including those in the IANA HTTP Status Code Registry.

Status code values in the range 100-999 (inclusive) are supported by this type. Values in the range 100-599 are semantically classified by the most significant digit. See StatusCode::is_success, etc. Values above 599 are unclassified but allowed for legacy compatibility, though their use is discouraged. Applications may interpret such values as protocol errors.

Examples

use http::StatusCode;

assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK);
assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404);
assert!(StatusCode::OK.is_success());

Implementations

Converts a u16 to a status code.

The function validates the correctness of the supplied u16. It must be greater or equal to 100 and less than 1000.

Example
use http::StatusCode;

let ok = StatusCode::from_u16(200).unwrap();
assert_eq!(ok, StatusCode::OK);

let err = StatusCode::from_u16(99);
assert!(err.is_err());

Converts a &u8 to a status code

Returns the u16 corresponding to this StatusCode.

Note

This is the same as the From<StatusCode> implementation, but included as an inherent method because that implementation doesn’t appear in rustdocs, as well as a way to force the type instead of relying on inference.

Example
let status = http::StatusCode::OK;
assert_eq!(status.as_u16(), 200);

Returns a &str representation of the StatusCode

The return value only includes a numerical representation of the status code. The canonical reason is not included.

Example
let status = http::StatusCode::OK;
assert_eq!(status.as_str(), "200");

Get the standardised reason-phrase for this status code.

This is mostly here for servers writing responses, but could potentially have application at other times.

The reason phrase is defined as being exclusively for human readers. You should avoid deriving any meaning from it at all costs.

Bear in mind also that in HTTP/2.0 and HTTP/3.0 the reason phrase is abolished from transmission, and so this canonical reason phrase really is the only reason phrase you’ll find.

Example
let status = http::StatusCode::OK;
assert_eq!(status.canonical_reason(), Some("OK"));

Check if status is within 100-199.

Check if status is within 200-299.

Check if status is within 300-399.

Check if status is within 400-499.

Check if status is within 500-599.

100 Continue [RFC7231, Section 6.2.1]

101 Switching Protocols [RFC7231, Section 6.2.2]

102 Processing [RFC2518]

201 Created [RFC7231, Section 6.3.2]

202 Accepted [RFC7231, Section 6.3.3]

203 Non-Authoritative Information [RFC7231, Section 6.3.4]

204 No Content [RFC7231, Section 6.3.5]

205 Reset Content [RFC7231, Section 6.3.6]

206 Partial Content [RFC7233, Section 4.1]

207 Multi-Status [RFC4918]

208 Already Reported [RFC5842]

226 IM Used [RFC3229]

300 Multiple Choices [RFC7231, Section 6.4.1]

301 Moved Permanently [RFC7231, Section 6.4.2]

303 See Other [RFC7231, Section 6.4.4]

304 Not Modified [RFC7232, Section 4.1]

305 Use Proxy [RFC7231, Section 6.4.5]

307 Temporary Redirect [RFC7231, Section 6.4.7]

308 Permanent Redirect [RFC7238]

400 Bad Request [RFC7231, Section 6.5.1]

401 Unauthorized [RFC7235, Section 3.1]

402 Payment Required [RFC7231, Section 6.5.2]

403 Forbidden [RFC7231, Section 6.5.3]

404 Not Found [RFC7231, Section 6.5.4]

405 Method Not Allowed [RFC7231, Section 6.5.5]

406 Not Acceptable [RFC7231, Section 6.5.6]

407 Proxy Authentication Required [RFC7235, Section 3.2]

408 Request Timeout [RFC7231, Section 6.5.7]

409 Conflict [RFC7231, Section 6.5.8]

411 Length Required [RFC7231, Section 6.5.10]

412 Precondition Failed [RFC7232, Section 4.2]

413 Payload Too Large [RFC7231, Section 6.5.11]

414 URI Too Long [RFC7231, Section 6.5.12]

415 Unsupported Media Type [RFC7231, Section 6.5.13]

416 Range Not Satisfiable [RFC7233, Section 4.4]

417 Expectation Failed [RFC7231, Section 6.5.14]

418 I’m a teapot [curiously not registered by IANA but RFC2324]

421 Misdirected Request RFC7540, Section 9.1.2

422 Unprocessable Entity [RFC4918]

423 Locked [RFC4918]

424 Failed Dependency [RFC4918]

426 Upgrade Required [RFC7231, Section 6.5.15]

428 Precondition Required [RFC6585]

429 Too Many Requests [RFC6585]

431 Request Header Fields Too Large [RFC6585]

451 Unavailable For Legal Reasons [RFC7725]

500 Internal Server Error [RFC7231, Section 6.6.1]

501 Not Implemented [RFC7231, Section 6.6.2]

502 Bad Gateway [RFC7231, Section 6.6.3]

503 Service Unavailable [RFC7231, Section 6.6.4]

504 Gateway Timeout [RFC7231, Section 6.6.5]

505 HTTP Version Not Supported [RFC7231, Section 6.6.6]

506 Variant Also Negotiates [RFC2295]

507 Insufficient Storage [RFC4918]

508 Loop Detected [RFC5842]

510 Not Extended [RFC2774]

511 Network Authentication Required [RFC6585]

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 status code, including the canonical reason.

Example

assert_eq!(format!("{}", StatusCode::OK), "200 OK");
Formats the value using the given formatter. Read more
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. 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 !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
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 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.
The type returned in the event of a conversion error.
Performs the conversion.

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.

Returns the argument unchanged.

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

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more