pub struct Request {Show 13 fields
pub method: String,
pub url: Url,
pub proto: String,
pub proto_major: u8,
pub proto_minor: u8,
pub header: Header,
pub body: Option<Body>,
pub content_length: i64,
pub transfer_encoding: Vec<String>,
pub host: String,
pub trailer: Header,
pub remote_addr: String,
pub path_params: HashMap<String, String>,
/* private fields */
}Expand description
An HTTP request (incoming server-side or outgoing client-side).
Mirrors Go’s http.Request.
Fields§
§method: StringHTTP method (GET, POST, …).
url: UrlParsed request URL.
proto: StringProtocol version string, e.g. “HTTP/1.1”.
proto_major: u8§proto_minor: u8§header: HeaderRequest headers.
body: Option<Body>Request body; None after the body has been consumed or for bodyless methods.
content_length: i64-1 means unknown; ≥ 0 means exact byte count from Content-Length.
transfer_encoding: Vec<String>Transfer-Encoding values in order (e.g. [“chunked”]).
host: StringValue of the Host header (or from the URL for outgoing requests).
trailer: HeaderTrailing headers populated after a chunked body is fully read.
remote_addr: StringRemote address of the client (set by the server, empty on client requests).
path_params: HashMap<String, String>Named path parameters captured by ServeMux wildcard patterns, e.g. {id}.
Populated by ServeMux::serve_http; empty for client-side requests.
Implementations§
Source§impl Request
impl Request
Sourcepub fn new(
method: &str,
url: &str,
body: Option<Body>,
) -> Result<Self, HttpError>
pub fn new( method: &str, url: &str, body: Option<Body>, ) -> Result<Self, HttpError>
Create a new outgoing request.
Port of Go’s http.NewRequest.
Sourcepub fn new_with_context(
method: &str,
url: &str,
body: Option<Body>,
ctx: Context,
) -> Result<Self, HttpError>
pub fn new_with_context( method: &str, url: &str, body: Option<Body>, ctx: Context, ) -> Result<Self, HttpError>
Create a new outgoing request tied to a context.
Port of Go’s http.NewRequestWithContext.
pub fn context(&self) -> &Context
Sourcepub fn with_context(self, ctx: Context) -> Self
pub fn with_context(self, ctx: Context) -> Self
Return a shallow clone of this request with the context replaced.
Port of Go’s (*Request).WithContext.
Sourcepub fn path_value(&self, name: &str) -> &str
pub fn path_value(&self, name: &str) -> &str
Return the value captured for the named wildcard in the matched ServeMux
pattern (e.g. {id} or {path...}). Returns "" if the parameter
was not captured. Port of Go’s (*Request).PathValue.
pub fn user_agent(&self) -> &str
pub fn referer(&self) -> &str
Sourcepub fn basic_auth(&self) -> Option<(String, String)>
pub fn basic_auth(&self) -> Option<(String, String)>
Parse and return Basic Auth credentials.
Port of Go’s (*Request).BasicAuth.
Return all cookies sent with the request.
Return the named cookie, or None.
Sourcepub fn body_bytes(&mut self) -> Result<Vec<u8>, HttpError>
pub fn body_bytes(&mut self) -> Result<Vec<u8>, HttpError>
Read the entire request body, populate self.trailer from any chunked
trailer headers, and return the raw bytes.
Sourcepub fn trailers(&self) -> &Header
pub fn trailers(&self) -> &Header
Trailer headers declared by the client. Populated after the body has
been fully read via body_bytes or a manual read_to_end.
Sourcepub fn parse_form(&mut self) -> Result<(), HttpError>
pub fn parse_form(&mut self) -> Result<(), HttpError>
Parse application/x-www-form-urlencoded body or query string.
Port of Go’s (*Request).ParseForm.
Sourcepub fn form_value(&self, key: &str) -> Option<&str>
pub fn form_value(&self, key: &str) -> Option<&str>
Return a form value by key (after calling parse_form).
Sourcepub fn write_header_to(&self, w: &mut impl Write) -> Result<(), HttpError>
pub fn write_header_to(&self, w: &mut impl Write) -> Result<(), HttpError>
Serialize the request line and headers to w (body not included).
Port of Go’s (*Request).write.
Sourcepub fn write_header_absolute_to(
&self,
w: &mut impl Write,
) -> Result<(), HttpError>
pub fn write_header_absolute_to( &self, w: &mut impl Write, ) -> Result<(), HttpError>
Like write_header_to but emits the
request-target in absolute-form (GET http://host/path HTTP/1.1),
as required when sending a request to an HTTP proxy (RFC 7230 §5.3.2).