#[non_exhaustive]pub struct Request {
pub address: Address,
pub method: Method,
pub path: Path,
pub query: QueryString,
pub headers: Headers,
pub body: RequestBody,
}Expand description
§Request
Encaspsulates the request data for an HTTP request.
A request consists of:
- address (scheme, host, port)
- method (GET, POST, etc.)
- path (the resource path)
- query string (key-value pairs for the query string)
- Headers (HTTP headers)
- body (the request payload)
A request starts in a default state with:
let request = Request::default();
assert_eq!(request.method, Method::Get);
assert_eq!(request.path.as_str(), "/");
assert!(request.query.is_empty());
assert!(request.headers.is_empty());
assert!(request.body.is_empty());
assert_eq!(request.build_url().as_str(), "http://localhost/");The structure is designed to be immutable, with builder patterns provided for construction and modification.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.address: Address§method: Method§path: Path§query: QueryString§headers: Headers§body: RequestBodyImplementations§
Source§impl Request
impl Request
Sourcepub fn builder() -> RequestBuilder
pub fn builder() -> RequestBuilder
§Request Builder
Starts a new request builder that can be used to create a Request instance.
let request = Request::builder()
.address_port(3000)
.method(Method::Post)
.path("/api/v1/resource")
.header("Content-Type", "application/json")
.body_text(r#"{"foo":"bar"}"#)
.build();
assert_eq!(request.method, Method::Post);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert_eq!(request.headers.first("Content-Type"), Some("application/json"));
assert_eq!(request.body.text(), Some(r#"{"foo":"bar"}"#));
assert_eq!(
request.build_url().as_str(),
"http://localhost:3000/api/v1/resource"
);Sourcepub fn factory() -> RequestBuilder<RequestFactory>
pub fn factory() -> RequestBuilder<RequestFactory>
§Request Factory
Prepares a request factory that can be used to create a base configuration for multiple requests.
let factory: RequestFactory = Request::factory()
.address_port(8080)
.address_scheme(Scheme::Https)
.header("Content-Type", "application/json")
.build();
let request = factory
.post("/api/v1/resource")
.body_text(r#"{"foo":"bar"}"#)
.build();
assert_eq!(request.method, Method::Post);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert_eq!(request.body.text(), Some(r#"{"foo":"bar"}"#));
assert_eq!(request.address.scheme, Scheme::Https);
assert_eq!(request.address.port, Some(8080));
assert_eq!(request.headers.first("Content-Type"), Some("application/json"));
// Small and easy to clone around to start a new request
assert_eq!(
std::mem::size_of::<RequestFactory>(),
std::mem::size_of::<usize>(),
);Trait Implementations§
Source§impl From<Request> for RequestFactory
impl From<Request> for RequestFactory
impl Eq for Request
impl StructuralPartialEq for Request
Auto Trait Implementations§
impl Freeze for Request
impl RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl UnsafeUnpin for Request
impl UnwindSafe for Request
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more