Struct StatusCode

pub struct StatusCode(/* private field */);

An HTTP status code (status-code in RFC 9110 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

impl StatusCode

const fn from_u16(src: u16) -> Result<StatusCode, InvalidStatusCode>

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());
fn from_bytes(src: &[u8]) -> Result<StatusCode, InvalidStatusCode>

Converts a &[u8] to a status code.

const fn as_u16(&self) -> u16

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);
fn as_str(&self) -> &str

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");
fn canonical_reason(&self) -> Option<&'static str>

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"));
fn is_informational(&self) -> bool

Check if status is within 100-199.

fn is_success(&self) -> bool

Check if status is within 200-299.

fn is_redirection(&self) -> bool

Check if status is within 300-399.

fn is_client_error(&self) -> bool

Check if status is within 400-499.

fn is_server_error(&self) -> bool

Check if status is within 500-599.

impl StatusCode

const CONTINUE: StatusCode = _;

100 Continue [RFC9110, Section 15.2.1]

const SWITCHING_PROTOCOLS: StatusCode = _;

101 Switching Protocols [RFC9110, Section 15.2.2]

const PROCESSING: StatusCode = _;

102 Processing [RFC2518, Section 10.1]

const EARLY_HINTS: StatusCode = _;

103 Early Hints [RFC8297, Section 2]

const OK: StatusCode = _;

200 OK [RFC9110, Section 15.3.1]

const CREATED: StatusCode = _;

201 Created [RFC9110, Section 15.3.2]

const ACCEPTED: StatusCode = _;

202 Accepted [RFC9110, Section 15.3.3]

const NON_AUTHORITATIVE_INFORMATION: StatusCode = _;

203 Non-Authoritative Information [RFC9110, Section 15.3.4]

const NO_CONTENT: StatusCode = _;

204 No Content [RFC9110, Section 15.3.5]

const RESET_CONTENT: StatusCode = _;

205 Reset Content [RFC9110, Section 15.3.6]

const PARTIAL_CONTENT: StatusCode = _;

206 Partial Content [RFC9110, Section 15.3.7]

const MULTI_STATUS: StatusCode = _;

207 Multi-Status [RFC4918, Section 11.1]

const ALREADY_REPORTED: StatusCode = _;

208 Already Reported [RFC5842, Section 7.1]

const IM_USED: StatusCode = _;

226 IM Used [RFC3229, Section 10.4.1]

const MULTIPLE_CHOICES: StatusCode = _;

300 Multiple Choices [RFC9110, Section 15.4.1]

const MOVED_PERMANENTLY: StatusCode = _;

301 Moved Permanently [RFC9110, Section 15.4.2]

const FOUND: StatusCode = _;

302 Found [RFC9110, Section 15.4.3]

const SEE_OTHER: StatusCode = _;

303 See Other [RFC9110, Section 15.4.4]

const NOT_MODIFIED: StatusCode = _;

304 Not Modified [RFC9110, Section 15.4.5]

const USE_PROXY: StatusCode = _;

305 Use Proxy [RFC9110, Section 15.4.6]

const TEMPORARY_REDIRECT: StatusCode = _;

307 Temporary Redirect [RFC9110, Section 15.4.7]

const PERMANENT_REDIRECT: StatusCode = _;

308 Permanent Redirect [RFC9110, Section 15.4.8]

const BAD_REQUEST: StatusCode = _;

400 Bad Request [RFC9110, Section 15.5.1]

const UNAUTHORIZED: StatusCode = _;

401 Unauthorized [RFC9110, Section 15.5.2]

const PAYMENT_REQUIRED: StatusCode = _;

402 Payment Required [RFC9110, Section 15.5.3]

const FORBIDDEN: StatusCode = _;

403 Forbidden [RFC9110, Section 15.5.4]

const NOT_FOUND: StatusCode = _;

404 Not Found [RFC9110, Section 15.5.5]

const METHOD_NOT_ALLOWED: StatusCode = _;

405 Method Not Allowed [RFC9110, Section 15.5.6]

const NOT_ACCEPTABLE: StatusCode = _;

406 Not Acceptable [RFC9110, Section 15.5.7]

const PROXY_AUTHENTICATION_REQUIRED: StatusCode = _;

407 Proxy Authentication Required [RFC9110, Section 15.5.8]

const REQUEST_TIMEOUT: StatusCode = _;

408 Request Timeout [RFC9110, Section 15.5.9]

const CONFLICT: StatusCode = _;

409 Conflict [RFC9110, Section 15.5.10]

const GONE: StatusCode = _;

410 Gone [RFC9110, Section 15.5.11]

const LENGTH_REQUIRED: StatusCode = _;

411 Length Required [RFC9110, Section 15.5.12]

const PRECONDITION_FAILED: StatusCode = _;

412 Precondition Failed [RFC9110, Section 15.5.13]

const PAYLOAD_TOO_LARGE: StatusCode = _;

413 Payload Too Large [RFC9110, Section 15.5.14]

const URI_TOO_LONG: StatusCode = _;

414 URI Too Long [RFC9110, Section 15.5.15]

const UNSUPPORTED_MEDIA_TYPE: StatusCode = _;

415 Unsupported Media Type [RFC9110, Section 15.5.16]

const RANGE_NOT_SATISFIABLE: StatusCode = _;

416 Range Not Satisfiable [RFC9110, Section 15.5.17]

const EXPECTATION_FAILED: StatusCode = _;

417 Expectation Failed [RFC9110, Section 15.5.18]

const IM_A_TEAPOT: StatusCode = _;

418 I'm a teapot [curiously not registered by IANA but RFC2324, Section 2.3.2]

const MISDIRECTED_REQUEST: StatusCode = _;

421 Misdirected Request [RFC9110, Section 15.5.20]

const UNPROCESSABLE_ENTITY: StatusCode = _;

422 Unprocessable Entity [RFC9110, Section 15.5.21]

const LOCKED: StatusCode = _;

423 Locked [RFC4918, Section 11.3]

const FAILED_DEPENDENCY: StatusCode = _;

424 Failed Dependency [RFC4918, Section 11.4]

const TOO_EARLY: StatusCode = _;

425 Too early [RFC8470, Section 5.2]

const UPGRADE_REQUIRED: StatusCode = _;

426 Upgrade Required [RFC9110, Section 15.5.22]

const PRECONDITION_REQUIRED: StatusCode = _;

428 Precondition Required [RFC6585, Section 3]

const TOO_MANY_REQUESTS: StatusCode = _;

429 Too Many Requests [RFC6585, Section 4]

const REQUEST_HEADER_FIELDS_TOO_LARGE: StatusCode = _;

431 Request Header Fields Too Large [RFC6585, Section 5]

451 Unavailable For Legal Reasons [RFC7725, Section 3]

const INTERNAL_SERVER_ERROR: StatusCode = _;

500 Internal Server Error [RFC9110, Section 15.6.1]

const NOT_IMPLEMENTED: StatusCode = _;

501 Not Implemented [RFC9110, Section 15.6.2]

const BAD_GATEWAY: StatusCode = _;

502 Bad Gateway [RFC9110, Section 15.6.3]

const SERVICE_UNAVAILABLE: StatusCode = _;

503 Service Unavailable [RFC9110, Section 15.6.4]

const GATEWAY_TIMEOUT: StatusCode = _;

504 Gateway Timeout [RFC9110, Section 15.6.5]

const HTTP_VERSION_NOT_SUPPORTED: StatusCode = _;

505 HTTP Version Not Supported [RFC9110, Section 15.6.6]

const VARIANT_ALSO_NEGOTIATES: StatusCode = _;

506 Variant Also Negotiates [RFC2295, Section 8.1]

const INSUFFICIENT_STORAGE: StatusCode = _;

507 Insufficient Storage [RFC4918, Section 11.5]

const LOOP_DETECTED: StatusCode = _;

508 Loop Detected [RFC5842, Section 7.2]

const NOT_EXTENDED: StatusCode = _;

510 Not Extended [RFC2774, Section 7]

const NETWORK_AUTHENTICATION_REQUIRED: StatusCode = _;

511 Network Authentication Required [RFC6585, Section 6]

Trait Implementations

impl Clone for StatusCode

fn clone(&self) -> StatusCode

impl Copy for StatusCode

impl Debug for StatusCode

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

impl Default for StatusCode

fn default() -> StatusCode

impl Display for StatusCode

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

impl Eq for StatusCode

impl From<&StatusCode> for StatusCode

fn from(t: &StatusCode) -> Self

impl FromStr for StatusCode

type Err = InvalidStatusCode;
fn from_str(s: &str) -> Result<StatusCode, InvalidStatusCode>

impl Hash for StatusCode

fn hash<__H: Hasher>(&self, state: &mut __H)

impl Ord for StatusCode

fn cmp(&self, other: &StatusCode) -> Ordering

impl PartialEq for StatusCode

fn eq(&self, other: &StatusCode) -> bool

impl PartialEq<u16> for StatusCode

fn eq(&self, other: &u16) -> bool

impl PartialOrd for StatusCode

fn partial_cmp(&self, other: &StatusCode) -> Option<Ordering>

impl StructuralPartialEq for StatusCode

impl TryFrom<&[u8]> for StatusCode

type Error = InvalidStatusCode;
fn try_from(t: &[u8]) -> Result<Self, Self::Error>

impl TryFrom<&str> for StatusCode

type Error = InvalidStatusCode;
fn try_from(t: &str) -> Result<Self, Self::Error>

impl TryFrom<u16> for StatusCode

type Error = InvalidStatusCode;
fn try_from(t: u16) -> Result<Self, Self::Error>

Auto Trait Implementations

impl Freeze for StatusCode

impl RefUnwindSafe for StatusCode

impl Send for StatusCode

impl Sync for StatusCode

impl Unpin for StatusCode

impl UnsafeUnpin for StatusCode

impl UnwindSafe for StatusCode

Blanket Implementations

impl<T> Any for StatusCode where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for StatusCode where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for StatusCode where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for StatusCode where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for StatusCode

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for StatusCode where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for StatusCode where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for StatusCode where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for StatusCode where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for StatusCode where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>