request
import std::net::http::request; · source
Types
Request
type struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
static new(method: Method, path: Str) -> Request;
param(&this, name: Str) -> Option<String>;
with_header(mut this, name: Str, value: Str) -> Request;
with_body(mut this, body: Array<u8>) -> Request;
set_body(mut &this, body: Array<u8>) -> void;
static from_request_line(line: Str) -> Result<Request, IoError>;
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;
}
Request::new(method, path) builds one; with_header(name, value) and with_body(body) consume and return it so they chain, and set_body(body) mutates in place. param(name) reads a router capture. encode_into(out) writes the complete HTTP/1.1 message — request line, headers, blank line, body.
mut req: Request = Request::new(Method::Post, Str::new("/api/items"))
.with_header(Str::new("Content-Type"), Str::new("application/json"))
.with_body(body);
from_request_line parses the first line of an incoming request; the header and body reads that follow it live in conn. The fields are public, so a handler can read req.headers or req.body directly. MAX_HEADER_LINE and MAX_BODY bound what a reader will accept.
Properties
| Property | Type | Notes |
|---|---|---|
method | Method | |
path | String | |
version | String | |
headers | Headers | |
body | Array<u8> | |
params | HashMap<String, String> | Path parameters captured by the router (/users/:id -> id). Empty for requests dispatched without a Router. |
Constructors
static new(method: Method, path: Str) -> Request;
static new(method: Method, path: Str) -> Request;Construct a minimal request. Clients build on top of this with with_header / with_body.
static from_request_line(line: Str) -> Result<Request, IoError>;
static from_request_line(line: Str) -> Result<Request, IoError>;Parse a Request-Line (METHOD SP PATH SP VERSION, no CRLF) into a request with empty headers and no body. Malformed input is InvalidData, which a server surfaces as 400.
Methods
param(&this, name: Str) -> Option<String>;
param(&this, name: Str) -> Option<String>;Look up a path parameter captured by the router (e.g., :id in /users/:id). Returns an owned copy of the captured segment The caller is responsible for dropping it. Returns None when the parameter was not captured (no router, or pattern did not include a segment with that name).
with_header(mut this, name: Str, value: Str) -> Request;
with_header(mut this, name: Str, value: Str) -> Request;Builder: insert a header and return the request so calls chain. Consumes and returns this (the owning fields are moved, not copied): Request::new(m, p).with_header(n, v).with_body(b).
with_body(mut this, body: Array<u8>) -> Request;
with_body(mut this, body: Array<u8>) -> Request;Builder: set the body (and Content-Length) and return the request.
set_body(mut &this, body: Array<u8>) -> void;
set_body(mut &this, body: Array<u8>) -> void;Replace the body and set Content-Length accordingly.
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;Append the whole message — request line, headers, blank line, body — to out, normally the connection's pending() buffer.
Trait implementations
implement trait Drop for struct Request
Constants
const HTTP_VERSION: Str = Str::new("HTTP/1.1");
const MAX_HEADER_LINE: u64 = 8192;
const MAX_BODY: u64 = 16777216;
const MAX_HEADER_COUNT: u64 = 100;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}HTTP request.
Request.method: Methodtype enum Method {
Get;
Head;
Post;
Put;
Delete;
Connect;
Options;
Trace;
Patch;
}HTTP method.
std::net::http::methodRequest.path: Stringtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringRequest.version: StringRequest.headers: Headerstype struct Headers {
inner: HashMap<String, String>;
}HTTP headers: case-insensitive name/value map.
std::net::http::headersRequest.body: Array<u8>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayRequest.params: HashMap<String, String>Path parameters captured by the router (/users/:id -> id). Empty for requests dispatched without a Router.
type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmapstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Request::new(method: Method, path: Str) -> RequestConstruct a minimal request. Clients build on top of this with with_header / with_body.
(parameter) method: Method(parameter) path: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}HTTP request.
std::net::http::requestRequest::param(&this, name: Str) -> Option<String>Look up a path parameter captured by the router (e.g., :id in /users/:id). Returns an owned copy of the captured segment The caller is responsible for dropping it. Returns None when the parameter was not captured (no router, or pattern did not include a segment with that name).
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) name: StrRequest::with_header(mut this, name: Str, value: Str) -> RequestBuilder: insert a header and return the request so calls chain. Consumes and returns this (the owning fields are moved, not copied): Request::new(m, p).with_header(n, v).with_body(b).
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language reference(parameter) value: StrRequest::with_body(mut this, body: Array<u8>) -> RequestBuilder: set the body (and Content-Length) and return the request.
(parameter) body: Array<u8>Request::set_body(mut &this, body: Array<u8>) -> voidReplace the body and set Content-Length accordingly.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
static Request::from_request_line(line: Str) -> Result<Request, IoError>Parse a Request-Line (METHOD SP PATH SP VERSION, no CRLF) into a request with empty headers and no body. Malformed input is InvalidData, which a server surfaces as 400.
(parameter) line: Strtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}Request::encode_into(&this, out: Array<u8>*) -> Result<(), IoError>Append the whole message — request line, headers, blank line, body — to out, normally the connection's pending() buffer.
(parameter) out: Array<u8>*primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut req: Requeststatic Request::new(method: Method, path: Str) -> RequestConstruct a minimal request. Clients build on top of this with with_header / with_body.
Method::Poststatic Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
Request::with_header(mut this, name: Str, value: Str) -> RequestBuilder: insert a header and return the request so calls chain. Consumes and returns this (the owning fields are moved, not copied): Request::new(m, p).with_header(n, v).with_body(b).
Request::with_body(mut this, body: Array<u8>) -> RequestBuilder: set the body (and Content-Length) and return the request.
std::net::http::requestconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const HTTP_VERSION: Str = Str::new("HTTP/1.1");const MAX_HEADER_LINE: u64 = 8192;const MAX_BODY: u64 = 16777216;const MAX_HEADER_COUNT: u64 = 100;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Request.method: MethodRequest.path: StringRequest.body: Array<u8>Request::set_body(mut &this, body: Array<u8>) -> voidReplace the body and set Content-Length accordingly.
std::net::http::requestRequest::param(&this, name: Str) -> Option<String>Look up a path parameter captured by the router (e.g., :id in /users/:id). Returns an owned copy of the captured segment The caller is responsible for dropping it. Returns None when the parameter was not captured (no router, or pattern did not include a segment with that name).
Request::encode_into(&this, out: Array<u8>*) -> Result<(), IoError>Append the whole message — request line, headers, blank line, body — to out, normally the connection's pending() buffer.
static Request::from_request_line(line: Str) -> Result<Request, IoError>Parse a Request-Line (METHOD SP PATH SP VERSION, no CRLF) into a request with empty headers and no body. Malformed input is InvalidData, which a server surfaces as 400.
const MAX_HEADER_LINE: u64 = 8192;const MAX_BODY: u64 = 16777216;Request.version: StringRequest.headers: HeadersRequest.params: HashMap<String, String>Path parameters captured by the router (/users/:id -> id). Empty for requests dispatched without a Router.
type struct Router {
routes: Array<Route>;
}Method- and path-based request dispatcher.
std::net::http::routerIoErrorKind::InvalidDataOption::Noneimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference