pub struct Response {
pub inner: InnerResponse,
pub body: Body,
pub metas: MetaResponse,
/* private fields */
}
Expand description
An Wrapper of http::Response
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
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.
Fields
inner: InnerResponse
inner parts of Response
body: Body
body
metas: MetaResponse
the metadata of the response
Implementations
sourceimpl Response
impl Response
sourcepub fn builder() -> ResponseBuilder
pub fn builder() -> ResponseBuilder
Creates a new builder-style object to manufacture a Response
This method returns an instance of ResponseBuilder
which can be used to
create a Response
.
Examples
let response = Response::builder()
.status(200)
.header("accept", "*/*")
.body(Body::empty())
.unwrap();
sourcepub fn new<T>(body: T) -> Response where
Body: From<T>,
pub fn new<T>(body: T) -> Response where
Body: From<T>,
Creates a new blank Response
with the body
The component ports of this response will be set to their default, e.g. the ok status, no headers, etc.
Examples
let response = Response::new("hello world");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body(), &Body::from("hello world"));
sourcepub fn from_parts(
inner: InnerResponse,
body: Body,
meta: MetaResponse
) -> Response
pub fn from_parts(
inner: InnerResponse,
body: Body,
meta: MetaResponse
) -> Response
Creates a new Response
with the given head and body
Examples
let response = Response::new("hello world");
let (mut inner, body, meta) = response.into_parts();
inner.status = StatusCode::BAD_REQUEST;
let response = Response::from_parts(inner, body, meta);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the StatusCode
.
Examples
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);
sourcepub fn status_mut(&mut self) -> &mut StatusCode
pub fn status_mut(&mut self) -> &mut StatusCode
Returns a mutable reference to the associated StatusCode
.
Examples
let mut response = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);
sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns a reference to the associated version.
Examples
let response: Response = Response::default();
assert_eq!(response.version(), Version::HTTP_11);
sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Returns a mutable reference to the associated version.
Examples
let mut response: Response = Response::default();
*response.version_mut() = Version::HTTP_2;
assert_eq!(response.version(), Version::HTTP_2);
sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
Get a share reference to the header of HeaderMap.
Examples
let response: Response = Response::default();
assert!(response.headers().is_empty());
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
Returns a mutable reference to the associated header field map.
Examples
let mut response: Response = Response::default();
response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
Examples
let response: Response = Response::default();
assert!(response.extensions().get::<i32>().is_none());
sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions.
Examples
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.extensions().get::<&'static str>(), Some(&"hello"));
sourcepub fn exts(&self) -> &Extensions
pub fn exts(&self) -> &Extensions
Returns a reference to the associated extensions of metadata.
Examples
let response: Response<()> = Response::default();
assert!(response.exts().get::<i32>().is_none());
sourcepub fn exts_mut(&mut self) -> &mut Extensions
pub fn exts_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions of metadata.
Examples
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.exts().get(), Some(&"hello"));
sourcepub fn body(&self) -> &Body
pub fn body(&self) -> &Body
Returns a reference to the associated HTTP body.
Examples
let response: Response = Response::default();
assert!(response.body().is_empty());
sourcepub fn body_mut(&mut self) -> &mut Body
pub fn body_mut(&mut self) -> &mut Body
Returns a mutable reference to the associated HTTP body.
Examples
let mut response: Response<String> = Response::default();
response.body_mut() = Body::from("hello world");
assert!(!response.body().is_empty());
sourcepub fn into_body(self) -> Body
pub fn into_body(self) -> Body
Consumes the response, returning just the body.
Examples
let response = Response::new(10);
let body = response.into_body();
assert_eq!(body, Body::from(10));
sourcepub fn into_parts(self) -> (InnerResponse, Body, MetaResponse)
pub fn into_parts(self) -> (InnerResponse, Body, MetaResponse)
Consumes the response returning the head, metadata and body parts.
Examples
let response: Response = Response::default();
let (parts, body, meta) = response.into_parts();
assert_eq!(parts.status, StatusCode::OK);
sourcepub fn map<F>(self, f: F) -> Response where
F: FnOnce(Body) -> Body,
pub fn map<F>(self, f: F) -> Response where
F: FnOnce(Body) -> Body,
Consumes the response returning a new response with body mapped to the return type of the passed in function.
Examples
let response = Response::builder().body("some string").unwrap();
let mapped_response: Response = response.map(|b| {
Body::from(b.bytes())
});
assert_eq!(mapped_response.body().bytes(), "some string".as_bytes());
sourceimpl Response
impl Response
sourcepub fn xpath(&mut self, xpath: &str) -> Vec<Node>
Available on crate feature xpath
only.
pub fn xpath(&mut self, xpath: &str) -> Vec<Node>
xpath
only.parse the ready response’s body with xpath
the feature xpath
must be enabled
let html = r#"<!doctype html>
<html>
<title>Hello dyer!</title>
<body> ... </body>
</html>"#;
let body = Body::from(html);
let mut res = Response::new(body);
let title = res.xpath("//title/text()");
assert_eq!(
title[0]
.get_content(),
"Hello dyer!"
);
Trait Implementations
impl Send for Response
Safety: the safety of InnerResponse and MetaResponse is addressed repectively, the body is obviously Send and Sync xpath related dom and context can only be called when Response is successful and step into parse Since we parse the response’s body in main thread, it should be Send and Sync
impl Sync for Response
Safety: the safety of InnerResponse and MetaResponse is addressed repectively, the body is obviously Send and Sync xpath related dom and context can only be called when Response is successful and step into parse Since we parse the response’s body in main thread, it should be Send and Sync
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more