Struct Response
pub struct Response<T> { /* private fields */ }
Represents an HTTP response
An HTTP response 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.
Typically you'll work with responses on the client side as the result of
sending a Request and on the server you'll be generating a Response to
send back to the client.
Examples
Creating a Response to return
use ;
A simple 404 handler
use ;
Or otherwise inspecting the result of a request:
use http::{Request, Response};
fn get(url: &str) -> http::Result<Response<()>> {
// ...
# panic!()
}
let response = get("https://www.rust-lang.org/").unwrap();
if !response.status().is_success() {
panic!("failed to get a successful response status!");
}
if let Some(date) = response.headers().get("Date") {
// we've got a `Date` header!
}
let body = response.body();
// ...
Deserialize a response of bytes via json:
use Response;
use de;
#
#
Or alternatively, serialize the body of a response to json
use Response;
use ser;
#
#
Implementations
impl Response<()>
fn builder() -> BuilderCreates a new builder-style object to manufacture a
ResponseThis method returns an instance of
Builderwhich can be used to create aResponse.Examples
# use *; let response = builder .status .header .body .unwrap;
impl<T> Response<T>
fn new(body: T) -> Response<T>Creates a new blank
Responsewith the bodyThe component parts of this response will be set to their default, e.g. the ok status, no headers, etc.
Examples
# use *; let response = new; assert_eq!; assert_eq!;fn from_parts(parts: Parts, body: T) -> Response<T>Creates a new
Responsewith the given head and bodyExamples
# use *; let response = new; let = response.into_parts; parts.status = BAD_REQUEST; let response = from_parts; assert_eq!; assert_eq!;fn status(&self) -> StatusCodeReturns the
StatusCode.Examples
# use *; let response: = default; assert_eq!;fn status_mut(&mut self) -> &mut StatusCodeReturns a mutable reference to the associated
StatusCode.Examples
# use *; let mut response: = default; *response.status_mut = CREATED; assert_eq!;fn version(&self) -> VersionReturns a reference to the associated version.
Examples
# use *; let response: = default; assert_eq!;fn version_mut(&mut self) -> &mut VersionReturns a mutable reference to the associated version.
Examples
# use *; let mut response: = default; *response.version_mut = HTTP_2; assert_eq!;fn headers(&self) -> &HeaderMap<HeaderValue>Returns a reference to the associated header field map.
Examples
# use *; let response: = 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 response: = default; response.headers_mut.insert; assert!;fn extensions(&self) -> &ExtensionsReturns a reference to the associated extensions.
Examples
# use *; let response: = default; assert!;fn extensions_mut(&mut self) -> &mut ExtensionsReturns a mutable reference to the associated extensions.
Examples
# use *; # use *; let mut response: = default; response.extensions_mut.insert; assert_eq!;fn body(&self) -> &TReturns a reference to the associated HTTP body.
Examples
# use *; let response: = default; assert!;fn body_mut(&mut self) -> &mut TReturns a mutable reference to the associated HTTP body.
Examples
# use *; let mut response: = default; response.body_mut.push_str; assert!;fn into_body(self) -> TConsumes the response, returning just the body.
Examples
# use Response; let response = new; let body = response.into_body; assert_eq!;fn into_parts(self) -> (Parts, T)Consumes the response returning the head and body parts.
Examples
# use *; let response: = default; let = response.into_parts; assert_eq!;fn map<F, U>(self, f: F) -> Response<U> where F: FnOnce(T) -> U,Consumes the response returning a new response with body mapped to the return type of the passed in function.
Examples
# use *; let response = builder.body.unwrap; let mapped_response: = response.map; assert_eq!;
Trait Implementations
impl<T: Clone> Clone for Response<T>
fn clone(&self) -> Response<T>
impl<T: Debug> Debug for Response<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: Default> Default for Response<T>
fn default() -> Response<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for Response<T>
impl<T> !UnwindSafe for Response<T>
impl<T> Freeze for Response<T>
where
T: Freeze,
impl<T> Send for Response<T>
where
T: Send,
impl<T> Sync for Response<T>
where
T: Sync,
impl<T> Unpin for Response<T>
where
T: Unpin,
impl<T> UnsafeUnpin for Response<T>
where
T: UnsafeUnpin,
Blanket Implementations
impl<T> Any for Response<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Response<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Response<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Response<T>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Response<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Response<T>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Response<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 Response<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Response<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>