pub enum StatusCode {
Ok,
Created,
NoContent,
BadRequest,
Unauthorized,
Forbidden,
NotFound,
InternalServerError,
NotImplemented,
BadGateway,
Unknown,
}
Expand description
Enumeration of HTTP status codes representing various HTTP response statuses
This enum includes common HTTP status codes that cover successful requests, client errors, and server errors. Each variant represents a specific HTTP status code.
§Variants
Ok
: HTTP 200, indicates that the request was successful and the server has successfully processed it.Created
: HTTP 201, indicates that the request was successful and the server has created a new resource.NoContent
: HTTP 204, indicates that the request was successful but no content is returned.BadRequest
: HTTP 400, indicates that the request is invalid or malformed and the server cannot understand it.Unauthorized
: HTTP 401, indicates that the request is unauthorized and requires authentication.Forbidden
: HTTP 403, indicates that the server understands the request but refuses to authorize it, usually due to insufficient permissions.NotFound
: HTTP 404, indicates that the server cannot find the requested resource.InternalServerError
: HTTP 500, indicates that the server encountered an internal error and cannot process the request.NotImplemented
: HTTP 501, indicates that the server does not support the functionality required to fulfill the request.BadGateway
: HTTP 502, indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.Unknown
: Represents an unknown status code, typically used when the status code is not recognized or is undefined.
Variants§
Ok
200 OK
Created
201 Created
NoContent
204 No Content
BadRequest
400 Bad Request
401 Unauthorized
Forbidden
403 Forbidden
NotFound
404 Not Found
InternalServerError
500 Internal Server Error
NotImplemented
501 Not Implemented
BadGateway
502 Bad Gateway
Unknown
Unknown status code
Implementations§
Source§impl StatusCode
The StatusCode
enum represents the HTTP status codes.
impl StatusCode
The StatusCode
enum represents the HTTP status codes.
It maps common HTTP status codes to their respective meanings. It provides methods to retrieve the corresponding numeric code as well as the associated status text. Additionally, it implements conversion from a string representation of the status code.
§Variants
Ok
: HTTP status 200, indicating a successful request.Created
: HTTP status 201, indicating that the request was successful and resulted in a resource creation.NoContent
: HTTP status 204, indicating that the request was successful, but there is no content to return.BadRequest
: HTTP status 400, indicating a bad request, often due to incorrect syntax or invalid data.Unauthorized
: HTTP status 401, indicating that authentication is required and has failed or not been provided.Forbidden
: HTTP status 403, indicating that the server understands the request but refuses to authorize it.NotFound
: HTTP status 404, indicating that the requested resource could not be found.InternalServerError
: HTTP status 500, indicating that the server encountered an internal error.NotImplemented
: HTTP status 501, indicating that the server does not support the functionality required to fulfill the request.BadGateway
: HTTP status 502, indicating that the server, while acting as a gateway or proxy, received an invalid response from an upstream server.Unknown
: A default variant for unrecognized or undefined status codes.
Sourcepub fn code(&self) -> u16
pub fn code(&self) -> u16
Returns the numeric HTTP status code associated with this status code variant.
This method returns the corresponding HTTP numeric status code based on the StatusCode
variant.
For example:
Self::Ok
returns 200.Self::BadRequest
returns 400.Self::Unknown
returns 0 (the default for unrecognized status codes).
§Parameters
&self
: A reference to theStatusCode
enum instance. This represents the specific variant of theStatusCode
enum that the method is called on.
§Return Value
u16
: The numeric HTTP status code associated with theStatusCode
variant. For example:Self::Ok
returns200
.Self::BadRequest
returns400
.Self::Unknown
returns0
.
Sourcepub fn phrase(code: usize) -> String
pub fn phrase(code: usize) -> String
Converts an HTTP status code to its corresponding textual description.
This method matches a given numeric HTTP status code and returns the corresponding
textual representation defined in the StatusCode
enum.
§Parameters
code
: Ausize
representing the HTTP status code to convert.
§Return Value
String
: A string representing the textual description of the HTTP status code. For example:200
returns"OK"
.404
returns"Not Found"
.- Unrecognized codes return
"Unknown"
.