pub struct Response { /* private fields */ }
Expand description

An HTTP response.

Examples

use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");

Implementations

Create a new response.

Get the status

Get a mutable reference to a header.

Get an HTTP header.

Remove a header.

Set an HTTP header.

Examples
use http_types::{Method, Response, StatusCode, Url};

let mut req = Response::new(StatusCode::Ok);
req.insert_header("Content-Type", "text/plain");

Append a header to the headers.

Unlike insert this function will not override the contents of a header, but insert a header if there aren’t any. Or else append to the existing list of headers.

Examples
use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.append_header("Content-Type", "text/plain");

Set the body reader.

Examples
use http_types::{Response, StatusCode};

let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Nori!");

Replace the response body with a new body, returning the old body.

Examples
use http_types::{Body, Method, Response, StatusCode, Url};

let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");

let mut body: Body = req.replace_body("Hello, Chashu");

let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");

Swaps the value of the body with another body, without deinitializing either one.

Examples
use http_types::{Body, Method, Response, StatusCode, Url};

let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");

let mut body = "Hello, Chashu!".into();
req.swap_body(&mut body);

let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");

Take the response body, replacing it with an empty body.

Examples
use http_types::{Body, Method, Response, StatusCode, Url};

let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello, Nori!");
let mut body: Body = req.take_body();

let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");

Read the body as a string.

This consumes the response. If you want to read the body without consuming the response, consider using the take_body method and then calling Body::into_string or using the Response’s AsyncRead implementation to read the body.

Examples
use async_std::io::Cursor;
use http_types::{Body, Method, Response, StatusCode, Url};

let mut res = Response::new(StatusCode::Ok);
let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, None);
res.set_body(body);
assert_eq!(&res.body_string().await.unwrap(), "Hello Nori");

Read the body as bytes.

This consumes the Response. If you want to read the body without consuming the response, consider using the take_body method and then calling Body::into_bytes or using the Response’s AsyncRead implementation to read the body.

Examples
use http_types::{Body, Method, Response, StatusCode, Url};

let bytes = vec![1, 2, 3];
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_bytes(bytes));

let bytes = res.body_bytes().await?;
assert_eq!(bytes, vec![1, 2, 3]);

Read the body as JSON.

This consumes the response. If you want to read the body without consuming the response, consider using the take_body method and then calling Body::into_json or using the Response’s AsyncRead implementation to read the body.

Examples
use http_types::convert::{Deserialize, Serialize};
use http_types::{Body, Method, Response, StatusCode, Url};

#[derive(Debug, Serialize, Deserialize)]
struct Cat {
    name: String,
}

let cat = Cat {
    name: String::from("chashu"),
};
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_json(&cat)?);

let cat: Cat = res.body_json().await?;
assert_eq!(&cat.name, "chashu");

Read the body as x-www-form-urlencoded.

This consumes the request. If you want to read the body without consuming the request, consider using the take_body method and then calling Body::into_json or using the Response’s AsyncRead implementation to read the body.

Examples
use http_types::convert::{Deserialize, Serialize};
use http_types::{Body, Method, Response, StatusCode, Url};

#[derive(Debug, Serialize, Deserialize)]
struct Cat {
    name: String,
}

let cat = Cat {
    name: String::from("chashu"),
};
let mut res = Response::new(StatusCode::Ok);
res.set_body(Body::from_form(&cat)?);

let cat: Cat = res.body_form().await?;
assert_eq!(&cat.name, "chashu");

Set the response MIME.

Get the current content type

Get the length of the body stream, if it has been set.

This value is set when passing a fixed-size object into as the body. E.g. a string, or a buffer. Consumers of this API should check this value to decide whether to use Chunked encoding, or set the response length.

Returns true if the set length of the body stream is zero, false otherwise.

Get the HTTP version, if one has been set.

Examples
use http_types::{Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
assert_eq!(res.version(), None);

res.set_version(Some(Version::Http2_0));
assert_eq!(res.version(), Some(Version::Http2_0));

Sets a string representation of the peer address that this response was sent to. This might take the form of an ip/fqdn and port or a local socket address.

Sets a string representation of the local address that this response was sent on. This might take the form of an ip/fqdn and port, or a local socket address.

Get the peer socket address for the underlying transport, if appropriate

Get the local socket address for the underlying transport, if appropriate

Set the HTTP version.

Examples
use http_types::{Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.set_version(Some(Version::Http2_0));

Set the status.

Sends trailers to the a receiver.

Receive trailers from a sender.

Returns true if sending trailers is in progress.

Sends an upgrade connection to the a receiver.

Receive an upgraded connection from a sender.

Returns true if a protocol upgrade is in progress.

An iterator visiting all header pairs in arbitrary order.

An iterator visiting all header pairs in arbitrary order, with mutable references to the values.

An iterator visiting all header names in arbitrary order.

An iterator visiting all header values in arbitrary order.

Returns a reference to the existing local.

Returns a mutuable reference to the existing local state.

Examples
use http_types::{Response, StatusCode, Version};

let mut res = Response::new(StatusCode::Ok);
res.ext_mut().insert("hello from the extension");
assert_eq!(res.ext().get(), Some(&"hello from the extension"));

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more

Attempt to read from the AsyncRead into buf. Read more

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more

Clone the response, resolving the body to Body::empty() and removing extensions.

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Returns a reference to the value corresponding to the supplied name.

Panics

Panics if the name is not present in Response.

The returned type after indexing.

Returns a reference to the value corresponding to the supplied name.

Panics

Panics if the name is not present in Response.

The returned type after indexing.

Converts this type into the (usually inferred) input type.

Returns a iterator of references over the remaining items.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Returns the contents of the internal buffer, filling it with more data if empty. Read more

Consumes amt buffered bytes. Read more

Reads all bytes and appends them into buf until the delimiter byte or EOF is found. Read more

Reads all bytes and appends them into buf until a newline (the 0xA byte) or EOF is found. Read more

Returns a stream over the lines of this byte stream. Read more

Returns a stream over the contents of this reader split on the specified byte. Read more

Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached. Read more

A convenience for calling [AsyncBufRead::consume] on Unpin IO types. Read more

Creates a future which will read all the bytes associated with this I/O object into buf until the delimiter byte or EOF is reached. This method is the async equivalent to BufRead::read_until. Read more

Creates a future which will read all the bytes associated with this I/O object into buf until a newline (the 0xA byte) or EOF is reached, This method is the async equivalent to BufRead::read_line. Read more

Returns a stream over the lines of this reader. This method is the async equivalent to BufRead::lines. Read more

Reads some bytes from the byte stream. Read more

Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more

Reads the entire contents and appends them to a Vec. Read more

Reads the entire contents and appends them to a String. Read more

Reads the exact number of bytes required to fill buf. Read more

Creates an adapter which will read at most limit bytes from it. Read more

Converts this [AsyncRead] into a [Stream] of bytes. Read more

Creates an adapter which will chain this stream with another. Read more

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more

Creates an adaptor which will chain this stream with another. Read more

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more

Creates a future which will read all the bytes from this AsyncRead. Read more

Creates a future which will read all the bytes from this AsyncRead. Read more

Helper method for splitting this read/write object into two halves. Read more

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more

Reads all bytes and appends them into buf until a newline (the 0xA byte) is reached. Read more

Returns a stream over the lines of this byte stream. Read more

Returns a stream over the contents of this reader split on the byte byte. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Reads some bytes from the byte stream. Read more

Like read, except that it reads into a slice of buffers. Read more

Reads all bytes from the byte stream. Read more

Reads all bytes from the byte stream and appends them into a string. Read more

Reads the exact number of bytes required to fill buf. Read more

Creates an adaptor which will read at most limit bytes from it. Read more

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to a Stream over its bytes. Read more

Creates an adaptor which will chain this stream with another. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

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.