Struct surf::Response[][src]

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

An HTTP response, returned by Request.

Implementations

Get the HTTP status code.

Examples
let res = surf::get("https://httpbin.org/get").await?;
assert_eq!(res.status(), 200);

Get the HTTP protocol version.

Examples
use surf::http::Version;

let res = surf::get("https://httpbin.org/get").await?;
assert_eq!(res.version(), Some(Version::Http1_1));

Get a header.

Examples
let res = surf::get("https://httpbin.org/get").await?;
assert!(res.header("Content-Length").is_some());

Get an HTTP header mutably.

Remove a header.

Insert an HTTP header.

Append an HTTP header.

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.

Get a response scoped extension value.

Set a response scoped extension value.

Get the response content type as a Mime.

Gets the Content-Type header and parses it to a Mime type.

Read more on MDN

Panics

This method will panic if an invalid MIME type was set as a header.

Examples
use surf::http::mime;
let res = surf::get("https://httpbin.org/json").await?;
assert_eq!(res.content_type(), Some(mime::JSON));

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.

Set the body reader.

Take the response body as a Body.

This method can be called after the body has already been taken or read, but will return an empty Body.

Useful for adjusting the whole body, such as in middleware.

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

Reads the entire request body into a byte buffer.

This method can be called after the body has already been read, but will produce an empty buffer.

Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

Examples
let mut res = surf::get("https://httpbin.org/get").await?;
let bytes: Vec<u8> = res.body_bytes().await?;

Reads the entire response body into a string.

This method can be called after the body has already been read, but will produce an empty buffer.

Encodings

If the “encoding” feature is enabled, this method tries to decode the body with the encoding that is specified in the Content-Type header. If the header does not specify an encoding, UTF-8 is assumed. If the “encoding” feature is disabled, Surf only supports reading UTF-8 response bodies. The “encoding” feature is enabled by default.

Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted because the encoding is unsupported or incorrect, an Err is returned.

Examples
let mut res = surf::get("https://httpbin.org/get").await?;
let string: String = res.body_string().await?;

Reads and deserialized the entire request body from json.

Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted as valid json for the target type T, an Err is returned.

Examples
#[derive(Deserialize, Serialize)]
struct Ip {
    ip: String
}

let mut res = surf::get("https://api.ipify.org?format=json").await?;
let Ip { ip } = res.body_json().await?;

Reads and deserialized the entire request body from form encoding.

Errors

Any I/O error encountered while reading the body is immediately returned as an Err.

If the body cannot be interpreted as valid json for the target type T, an Err is returned.

Examples
#[derive(Deserialize, Serialize)]
struct Body {
    apples: u32
}

let mut res = surf::get("https://api.example.com/v1/response").await?;
let Body { apples } = res.body_form().await?;

Trait Implementations

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

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

Formats the value using the given formatter. Read more

Performs the conversion.

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.

Performs the conversion.

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

Performs the conversion.

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

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

Performs the conversion.

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

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