Struct Request
pub struct Request<T> { /* private fields */ }
Represents an HTTP request.
An HTTP request consists of a head and a potentially optional body. The body
component is generic, enabling arbitrary types to represent the HTTP body.
For example, the body could be Vec<u8>, a Stream of byte chunks, or a
value that has been deserialized.
Examples
Creating a Request to send
use http::{Request, Response};
let mut request = Request::builder()
.uri("https://www.rust-lang.org/")
.header("User-Agent", "my-awesome-agent/1.0");
if needs_awesome_header() {
request = request.header("Awesome", "yes");
}
let response = send(request.body(()).unwrap());
# fn needs_awesome_header() -> bool {
# true
# }
#
fn send(req: Request<()>) -> Response<()> {
// ...
# panic!()
}
Inspecting a request to see what was sent.
use ;
Deserialize a request of bytes via json:
use Request;
use de;
#
#
Or alternatively, serialize the body of a request to json
use Request;
use ser;
#
#
Implementations
impl Request<()>
fn builder() -> BuilderCreates a new builder-style object to manufacture a
RequestThis method returns an instance of
Builderwhich can be used to create aRequest.Examples
# use *; let request = builder .method .uri .header .body .unwrap;fn get<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a GET method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = get .body .unwrap;fn put<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a PUT method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = put .body .unwrap;fn post<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a POST method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = post .body .unwrap;fn delete<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a DELETE method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = delete .body .unwrap;fn options<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with an OPTIONS method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = options .body .unwrap; # assert_eq!;fn head<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a HEAD method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = head .body .unwrap;fn connect<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a CONNECT method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = connect .body .unwrap;fn patch<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a PATCH method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = patch .body .unwrap;fn trace<T>(uri: T) -> Builder where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<Error>,Creates a new
Builderinitialized with a TRACE method and the given URI.This method returns an instance of
Builderwhich can be used to create aRequest.Example
# use *; let request = trace .body .unwrap;
impl<T> Request<T>
fn new(body: T) -> Request<T>Creates a new blank
Requestwith the bodyThe component parts of this request will be set to their default, e.g. the GET method, no headers, etc.
Examples
# use *; let request = new; assert_eq!; assert_eq!;fn from_parts(parts: Parts, body: T) -> Request<T>Creates a new
Requestwith the given components parts and body.Examples
# use *; let request = new; let = request.into_parts; parts.method = POST; let request = from_parts;fn method(&self) -> &MethodReturns a reference to the associated HTTP method.
Examples
# use *; let request: = default; assert_eq!;fn method_mut(&mut self) -> &mut MethodReturns a mutable reference to the associated HTTP method.
Examples
# use *; let mut request: = default; *request.method_mut = PUT; assert_eq!;fn uri(&self) -> &UriReturns a reference to the associated URI.
Examples
# use *; let request: = default; assert_eq!;fn uri_mut(&mut self) -> &mut UriReturns a mutable reference to the associated URI.
Examples
# use *; let mut request: = default; *request.uri_mut = "/hello".parse.unwrap; assert_eq!;fn version(&self) -> VersionReturns the associated version.
Examples
# use *; let request: = default; assert_eq!;fn version_mut(&mut self) -> &mut VersionReturns a mutable reference to the associated version.
Examples
# use *; let mut request: = default; *request.version_mut = HTTP_2; assert_eq!;fn headers(&self) -> &HeaderMap<HeaderValue>Returns a reference to the associated header field map.
Examples
# use *; let request: = default; assert!;fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>Returns a mutable reference to the associated header field map.
Examples
# use *; # use *; let mut request: = default; request.headers_mut.insert; assert!;fn extensions(&self) -> &ExtensionsReturns a reference to the associated extensions.
Examples
# use *; let request: = default; assert!;fn extensions_mut(&mut self) -> &mut ExtensionsReturns a mutable reference to the associated extensions.
Examples
# use *; # use *; let mut request: = default; request.extensions_mut.insert; assert_eq!;fn body(&self) -> &TReturns a reference to the associated HTTP body.
Examples
# use *; let request: = default; assert!;fn body_mut(&mut self) -> &mut TReturns a mutable reference to the associated HTTP body.
Examples
# use *; let mut request: = default; request.body_mut.push_str; assert!;fn into_body(self) -> TConsumes the request, returning just the body.
Examples
# use Request; let request = new; let body = request.into_body; assert_eq!;fn into_parts(self) -> (Parts, T)Consumes the request returning the head and body parts.
Examples
# use *; let request = new; let = request.into_parts; assert_eq!;fn map<F, U>(self, f: F) -> Request<U> where F: FnOnce(T) -> U,Consumes the request returning a new request with body mapped to the return type of the passed in function.
Examples
# use *; let request = builder.body.unwrap; let mapped_request: = request.map; assert_eq!;
Trait Implementations
impl<T: Clone> Clone for Request<T>
fn clone(&self) -> Request<T>
impl<T: Debug> Debug for Request<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: Default> Default for Request<T>
fn default() -> Request<T>
Auto Trait Implementations
impl<T> !Freeze for Request<T>
impl<T> !RefUnwindSafe for Request<T>
impl<T> !UnwindSafe for Request<T>
impl<T> Send for Request<T>
where
T: Send,
impl<T> Sync for Request<T>
where
T: Sync,
impl<T> Unpin for Request<T>
where
T: Unpin,
impl<T> UnsafeUnpin for Request<T>
where
T: UnsafeUnpin,
Blanket Implementations
impl<T> Any for Request<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Request<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Request<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Request<T>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Request<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Request<T>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Request<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Request<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Request<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>