Struct tide::Response[][src]

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

An HTTP response

Implementations

Create a new instance.

Begin a chained response builder. For more details, see ResponseBuilder

Example:
let mut response = Response::builder(203)
    .body("<html>hi</html>")
    .header("custom-header", "value")
    .content_type(mime::HTML)
    .build();

assert_eq!(response.take_body().into_string().await.unwrap(), "<html>hi</html>");
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
assert_eq!(response["custom-header"], "value");
assert_eq!(response["content-type"], "text/html;charset=utf-8");

Returns the http status code.

Set the http status code.

Example:
let mut response = Response::new(StatusCode::Ok);

response.set_status(418); // the status can be a valid u16 http status code
assert_eq!(response.status(), StatusCode::ImATeapot);

response.set_status(StatusCode::NonAuthoritativeInformation); // or a tide::StatusCode
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
Panics

set_status will panic if the status argument cannot be successfully converted into a StatusCode.

Response::new(200).set_status(210); // this is not an established status code and will panic

Get the length of the body.

Checks if the body is empty.

Get an HTTP header.

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 the response content type as a Mime.

This gets the request Content-Type header.

Read more on MDN

Set the response content type from a MIME.

This sets the response Content-Type header.

Read more on MDN

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.

Examples
use tide::Response;

let mut req = Response::new(200);
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!");

Pass JSON as the response body.

Mime

The Content-Type is set to application/json.

Errors

This method will return an error if the provided data could not be serialized to JSON.

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

let data = &Ip { ip: "129.0.0.1".into() };
let mut res = Response::new(200);
res.body_json(data)?;

Pass a string as the response body.

Mime

The Content-Type is set to text/plain; charset=utf-8.

Examples
let data = "hello world".to_string();
let mut res = Response::new(200);
res.body_string(data);

Pass bytes as the request body.

Mime

The Content-Type is set to application/octet-stream.

Examples
let data = b"hello world".to_owned();
let mut res = Response::new(200);
res.body_bytes(data);

Pass a file as the response body.

Mime

The Content-Type is set based on the file extension using mime_guess if the operation was successful. If path has no extension, or its extension has no known MIME type mapping, then None is returned.

Errors

This method will return an error if the file couldn’t be read.

Examples
let mut res = Response::new(200);
res.body_file("./archive.tgz").await?;

Insert cookie in the cookie jar.

Removes the cookie. This instructs the CookiesMiddleware to send a cookie with empty value in the response.

Warning

Take care when calling this function with a cookie that was returned by Request::cookie. As per section 5.3 step 11 of RFC 6265, a new cookie is only treated as the same as an old one if it has a matching name, domain and path.

The domain and path are not sent to the server on subsequent HTTP requests, so if a cookie was originally set with a domain and/or path, calling this function on a cookie with the same name but with either a different, or no, domain and/or path will lead to us sending an empty cookie that the user agent will treat as unrelated to the original one, and will thus not remove the old one.

To avoid this you can manually set the domain and path as necessary after retrieving the cookie using Request::cookie.

Returns an optional reference to an error if the response contains one.

Returns a reference to the original error associated with this response if there is one and if it can be downcast to the specified type.

Example
use tide::Response;

let error = std::io::Error::new(ErrorKind::Other, "oh no!");
let error = tide::http::Error::from(error);

let mut res = Response::new(400);
res.set_error(error);

if let Some(err) = res.downcast_error::<std::io::Error>() {
  // Do something with the `std::io::Error`.
}

Takes the error from the response if one exists, replacing it with None.

Sets the response’s error, overwriting any existing error.

This is particularly useful for middleware which would like to notify further middleware that an error has occurred without overwriting the existing response.

Get a response scoped extension value.

Set a response scoped extension value.

Create a tide::Response from a type that can be converted into an http_types::Response.

Trait Implementations

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

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.

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

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

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.