HTTP Models
HTTP protocol models and types for methods, status codes, and framework integration.
Overview
The HTTP Models package provides:
- HTTP Methods: Complete HTTP method enumeration with parsing
- Status Codes: Comprehensive HTTP status code definitions
- Framework Integration: Actix Web and Reqwest compatibility layers
- Type Safety: Strong typing for HTTP protocol elements
- Serialization: Serde support for JSON/API integration
Features
HTTP Methods
- Complete Method Set: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, CONNECT, TRACE
- Case-insensitive Parsing: Parse from various string formats
- Display Implementation: Convert methods back to strings
- Serde Integration: JSON serialization/deserialization
HTTP Status Codes
- Full Status Code Set: All standard HTTP status codes (100-511)
- Category Helpers: Check if status is success, error, redirect, etc.
- Numeric Conversion: Convert to/from u16 values
- MDN Documentation: Based on Mozilla Developer Network reference
Framework Integration
- Actix Web: Conversion traits for actix-web types
- Reqwest: Conversion traits for reqwest HTTP client
- Generic Support: Works with any HTTP framework
Installation
Add this to your Cargo.toml:
[]
= { = "../http/models" }
# With framework support
= {
path = "../http/models",
= ["actix", "reqwest", "serde"]
}
Usage
HTTP Methods
use Method;
use FromStr;
// Create methods
let get = Get;
let post = Post;
// Parse from strings (case-insensitive)
let method = from_str?;
let method = from_str?;
let method = from_str?;
// Convert to string
println!; // "GET"
println!; // "POST"
// Use in match expressions
match method
HTTP Status Codes
use StatusCode;
// Create status codes
let ok = Ok;
let not_found = NotFound;
let internal_error = InternalServerError;
// Convert to/from numeric values
let code: u16 = Ok.into; // 200
let status = try_from?; // StatusCode::NotFound
// Category checking
assert!;
assert!;
assert!;
assert!;
assert!;
// Display status codes
println!; // "OK"
println!; // "NOT_FOUND"
Status Code Categories
use StatusCode;
// Informational (1xx)
assert!;
assert!;
// Success (2xx)
assert!;
assert!;
assert!;
// Redirection (3xx)
assert!;
assert!;
assert!;
// Client Error (4xx)
assert!;
assert!;
assert!;
// Server Error (5xx)
assert!;
assert!;
assert!;
Serde Integration
use ;
use ;
// Serialize to JSON
let request = HttpRequest ;
let json = to_string?;
// {"method":"POST","path":"/api/users"}
let response = HttpResponse ;
let json = to_string?;
// {"status":"CREATED","body":"User created"}
Actix Web Integration
use *;
use ;
async
Reqwest Integration
use *;
async
Complete Status Code List
Informational (1xx)
Continue(100)SwitchingProtocols(101)Processing(102)EarlyHints(103)
Success (2xx)
Ok(200)Created(201)Accepted(202)NonAuthoritativeInformation(203)NoContent(204)ResetContent(205)PartialContent(206)MultiStatus(207)AlreadyReported(208)IMUsed(226)
Redirection (3xx)
MultipleChoices(300)MovedPermanently(301)Found(302)SeeOther(303)NotModified(304)UseProxy(305)TemporaryRedirect(307)PermanentRedirect(308)
Client Error (4xx)
BadRequest(400)Unauthorized(401)PaymentRequired(402)Forbidden(403)NotFound(404)MethodNotAllowed(405)NotAcceptable(406)ProxyAuthenticationRequired(407)RequestTimeout(408)Conflict(409)Gone(410)LengthRequired(411)PreconditionFailed(412)ContentTooLarge(413)URITooLong(414)UnsupportedMediaType(415)RangeNotSatisfiable(416)ExpectationFailed(417)ImATeapot(418)MisdirectedRequest(421)UncompressableContent(422)Locked(423)FailedDependency(424)TooEarly(425)UpgradeRequired(426)PreconditionRequired(428)TooManyRequests(429)RequestHeaderFieldsTooLarge(431)UnavailableForLegalReasons(451)
Server Error (5xx)
InternalServerError(500)NotImplemented(501)BadGateway(502)ServiceUnavailable(503)GatewayTimeout(504)HTTPVersionNotSupported(505)VariantAlsoNegotiates(506)InsufficientStorage(507)LoopDetected(508)NotExtended(510)NetworkAuthenticationRequired(511)
Error Handling
use ;
use FromStr;
// Method parsing errors
match from_str
// Status code conversion errors
match try_from
Feature Flags
serde: Enable JSON serialization/deserializationactix: Enable Actix Web integrationreqwest: Enable Reqwest HTTP client integration
Dependencies
- Serde: Serialization support (optional)
- Strum: Enum utilities for string conversion
- Thiserror: Error handling
Use Cases
- HTTP Client Libraries: Type-safe HTTP method and status handling
- Web Frameworks: Request/response type safety
- API Development: Consistent HTTP protocol handling
- Testing: Mock HTTP responses with proper types
- Logging: Structured logging of HTTP requests/responses