Skip to main content

Request

Struct Request 

Source
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: String

HTTP method (GET, POST, …).

§url: Url

Parsed request URL.

§proto: String

Protocol version string, e.g. “HTTP/1.1”.

§proto_major: u8§proto_minor: u8§header: Header

Request 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: String

Value of the Host header (or from the URL for outgoing requests).

§trailer: Header

Trailing headers populated after a chunked body is fully read.

§remote_addr: String

Remote 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

Source

pub fn new( method: &str, url: &str, body: Option<Body>, ) -> Result<Self, HttpError>

Create a new outgoing request. Port of Go’s http.NewRequest.

Source

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.

Source

pub fn context(&self) -> &Context

Source

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.

Source

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.

Source

pub fn user_agent(&self) -> &str

Source

pub fn referer(&self) -> &str

Source

pub fn basic_auth(&self) -> Option<(String, String)>

Parse and return Basic Auth credentials. Port of Go’s (*Request).BasicAuth.

Source

pub fn cookies(&self) -> Vec<Cookie>

Return all cookies sent with the request.

Source

pub fn cookie(&self, name: &str) -> Option<Cookie>

Return the named cookie, or None.

Source

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.

Source

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.

Source

pub fn parse_form(&mut self) -> Result<(), HttpError>

Parse application/x-www-form-urlencoded body or query string. Port of Go’s (*Request).ParseForm.

Source

pub fn form_value(&self, key: &str) -> Option<&str>

Return a form value by key (after calling parse_form).

Source

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.

Source

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).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.