Struct hypers::prelude::Response

source ·
pub struct Response {
    pub cookies: CookieJar,
    /* private fields */
}

Fields§

§cookies: CookieJar

Implementations§

source§

impl Response

source

pub fn new(body: impl Into<Bytes>) -> Response

source

pub fn cookie(&mut self, cookie: Cookie<'static>) -> &mut Response

Add cookie

source

pub fn cookies(&self) -> &CookieJar

source

pub fn cookies_mut(&mut self) -> &mut CookieJar

source

pub fn cookie_remove(&mut self, name: &str) -> &mut Response

Removes cookie from this CookieJar. Read more about removal cookies.

source

pub fn header<K, V>(&mut self, name: K, value: V) -> &mut Response

Appends a header to this response . This function will append the provided key/value as a header to the internal HeaderMap being constructed. Essentially this is equivalent to calling HeaderMap::append.

§Examples

let response = Response::default()
    .header("Content-Type", "text/html")
    .header("X-Custom-Foo", "bar")
    .header("content-length", 0)
    .body("");
source

pub fn header_typed<H>(&mut self, h: H) -> &mut Response
where H: Header,

Inserts a typed header to this response.

source

pub fn status<T>(&mut self, status: T) -> &mut Response
where T: TryInto<StatusCode>,

Sets the HTTP status for this response. By default this is StatusCode::OK.

source

pub fn redirect<T>(&mut self, status: T, url: &str) -> &mut Response
where T: TryInto<StatusCode>,

The response redirects to the specified URL. [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect

source

pub fn location(&mut self, location: &str) -> &mut Response

The [Content-Location][mdn] header indicates an alternate location for the returned data. [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location

source

pub fn body(&mut self, bytes: impl Into<Bytes>) -> &mut Response

Write bytes data to body

source

pub fn content_type(&mut self, content_type: &str) -> &mut Response

The response with the specified [Content-Type][mdn]. [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type

source

pub fn content_type_str(&self) -> Result<&str, Box<dyn Error + Sync + Send>>

Get content type. return Result<&str, Error>

source

pub fn json<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/json; charset=utf-8 media type.

source

pub fn json_pretty<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/json; charset=utf-8 media type.

source

pub fn form<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/x-www-form-urlencoded; charset=utf-8 media type.

source

pub fn yaml<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/yaml; charset=utf-8 media type.

source

pub fn toml<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/toml; charset=utf-8 media type.

source

pub fn xml<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/xml; charset=utf-8 media type.

source

pub fn msgpack<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/msgpack; charset=utf-8 media type.

source

pub fn cbor<T>(&mut self, value: &T) -> &mut Response
where T: Serialize,

The response with application/cbor; charset=utf-8 media type.

source

pub fn render<R>(self, responder: R) -> Response
where R: Responder,

Render content.

§Example
use hypers_core::prelude::Response;

let mut res = Response::default();
res.render("hello world");
source

pub fn stream<S, O, E>(&mut self, stream: S) -> &mut Response
where S: Stream<Item = Result<O, E>> + Send + 'static, O: Into<Bytes> + 'static, E: Into<Box<dyn Error + Sync + Send>> + 'static,

Responds to a stream.

source

pub async fn file<P>( &mut self, file_path: P, req_headers: &HeaderMap, ) -> &mut Response
where P: Into<PathBuf> + Send,

file writes the specified file into the body stream in an efficient way. If file not exists, not found error will occur.

source

pub fn download(&mut self, file_path: impl AsRef<Path>) -> &mut Response

download writes the specified file into the body stream in an efficient way. On the client side, the file will typically be downloaded with the given filename.

source

pub fn take_body(&mut self) -> Body

Take body form the request

source

pub async fn payload(&mut self) -> Result<Bytes, Box<dyn Error + Sync + Send>>

Get Bytes from response payload

source

pub fn into_hyper(self) -> Result<Response<Body>, InvalidHeaderValue>

Convert to hyper response.

source

pub fn map<F>(self, f: F) -> Response
where F: FnOnce(Body) -> Body,

source§

impl Response

source

pub fn assert_status<T>(&mut self, status: T)
where T: TryInto<StatusCode>,

Asserts that the status code is equals to status.

source

pub fn assert_status_is_ok(&mut self)

Asserts that the status code is 200 OK.

source

pub fn assert_header<K, V>(&self, key: K, value: V)

Asserts that header key is equals to value.

source

pub fn assert_content_type(&self, content_type: &str)

Asserts that content type is equals to content_type.

source

pub async fn assert_body(&mut self, bytes: impl Into<Bytes>)

Asserts that the response body is bytes::Bytes and it equals to bytes::Bytes.

source

pub async fn assert_json(&mut self, json: &impl Serialize)

Asserts that the response body is JSON and it equals to json.

source

pub async fn assert_xml(&mut self, xml: &impl Serialize)

Asserts that the response body is XML and it equals to xml.

source

pub async fn assert_yaml(&mut self, yaml: &impl Serialize)

Asserts that the response body is yaml and it equals to yaml.

source

pub async fn take_string( &mut self, ) -> Result<String, Box<dyn Error + Sync + Send>>

source

pub async fn take_json<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_form<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_yaml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_toml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_xml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_msgpack<T>( &mut self, ) -> Result<T, Box<dyn Error + Sync + Send>>

source

pub async fn take_cbor<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>

Methods from Deref<Target = Response<Body>>§

source

pub fn status(&self) -> StatusCode

Returns the StatusCode.

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

pub fn status_mut(&mut self) -> &mut StatusCode

Returns a mutable reference to the associated StatusCode.

§Examples
let mut response: Response<()> = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);
source

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

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

pub fn headers(&self) -> &HeaderMap

Returns a reference to the associated header field map.

§Examples
let response: Response<()> = Response::default();
assert!(response.headers().is_empty());
source

pub fn headers_mut(&mut self) -> &mut HeaderMap

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

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

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(), Some(&"hello"));
source

pub fn body(&self) -> &T

Returns a reference to the associated HTTP body.

§Examples
let response: Response<String> = Response::default();
assert!(response.body().is_empty());
source

pub fn body_mut(&mut self) -> &mut T

Returns a mutable reference to the associated HTTP body.

§Examples
let mut response: Response<String> = Response::default();
response.body_mut().push_str("hello world");
assert!(!response.body().is_empty());

Trait Implementations§

source§

impl Debug for Response

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Response

source§

fn default() -> Response

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

impl Deref for Response

source§

type Target = Response<Body>

The resulting type after dereferencing.
source§

fn deref(&self) -> &<Response as Deref>::Target

Dereferences the value.
source§

impl DerefMut for Response

source§

fn deref_mut(&mut self) -> &mut <Response as Deref>::Target

Mutably dereferences the value.
source§

impl HandlerOut for Response

source§

fn register(_: &mut Components, _: &mut Operation)

Modify the OpenApi components section or current operation information with given argument. This function is called by macros internal.
source§

impl Responder for Response

source§

impl SessionDepotExt for Response

source§

fn set_session(&mut self, session: Session) -> &mut Response

Sets session
source§

fn take_session(&mut self) -> Option<Session>

Take session
source§

fn session(&self) -> Option<&Session>

Get session reference
source§

fn session_mut(&mut self) -> Option<&mut Session>

Get session mutable reference

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

source§

type Output = T

Should always be Self
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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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