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

Fields

inner: InnerResponse

inner parts of Response

body: Body

body

metas: MetaResponse

the metadata of the response

Implementations

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();

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"));

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

Returns the StatusCode.

Examples
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);

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

Returns a reference to the associated version.

Examples
let response: Response = Response::default();
assert_eq!(response.version(), Version::HTTP_11);

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

Get a share reference to the header of HeaderMap.

Examples
let response: Response = Response::default();
assert!(response.headers().is_empty());

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());

Returns a reference to the associated extensions.

Examples
let response: Response = Response::default();
assert!(response.extensions().get::<i32>().is_none());

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"));

Returns a reference to the associated extensions of metadata.

Examples
let response: Response<()> = Response::default();
assert!(response.exts().get::<i32>().is_none());

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"));

Returns a reference to the associated HTTP body.

Examples
let response: Response = Response::default();
assert!(response.body().is_empty());

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());

Consumes the response, returning just the body.

Examples
let response = Response::new(10);
let body = response.into_body();
assert_eq!(body, Body::from(10));

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

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());
Available on crate feature 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

Returns the “default value” for a type. Read more

Converts to this type from the input type.

The type returned in the event of a conversion error.

Performs the conversion.

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

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

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more