Struct StatusCode

struct StatusCode(_)

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

impl StatusCode

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: &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: &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: &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: &Self) -> bool

Check if status is within 100-199.

fn is_success(self: &Self) -> bool

Check if status is within 200-299.

fn is_redirection(self: &Self) -> bool

Check if status is within 300-399.

fn is_client_error(self: &Self) -> bool

Check if status is within 400-499.

fn is_server_error(self: &Self) -> bool

Check if status is within 500-599.

impl Clone for StatusCode

fn clone(self: &Self) -> StatusCode

impl Copy for StatusCode

impl Debug for StatusCode

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

impl Default for StatusCode

fn default() -> StatusCode

impl Display for StatusCode

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

impl Eq for StatusCode

impl Freeze for StatusCode

impl FromStr for StatusCode

fn from_str(s: &str) -> Result<StatusCode, InvalidStatusCode>

impl Hash for StatusCode

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

impl Ord for StatusCode

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

impl PartialEq for StatusCode

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

impl PartialEq for StatusCode

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

impl PartialOrd for StatusCode

fn partial_cmp(self: &Self, other: &StatusCode) -> $crate::option::Option<$crate::cmp::Ordering>

impl RefUnwindSafe for StatusCode

impl Send for StatusCode

impl StructuralPartialEq for StatusCode

impl Sync for StatusCode

impl TryFrom for StatusCode

fn try_from(t: u16) -> Result<Self, <Self as >::Error>

impl Unpin for StatusCode

impl UnwindSafe for StatusCode

impl<'a> From for StatusCode

fn from(t: &'a StatusCode) -> Self

impl<'a> TryFrom for StatusCode

fn try_from(t: &'a str) -> Result<Self, <Self as >::Error>

impl<'a> TryFrom for StatusCode

fn try_from(t: &'a [u8]) -> Result<Self, <Self as >::Error>

impl<T> Any for StatusCode

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for StatusCode

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for StatusCode

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

impl<T> CloneToUninit for StatusCode

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

impl<T> From for StatusCode

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for StatusCode

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for StatusCode

fn to_string(self: &Self) -> String

impl<T, U> Into for StatusCode

fn into(self: 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 for StatusCode

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

impl<T, U> TryInto for StatusCode

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