Skip to main content

ResponseData

Struct ResponseData 

Source
pub struct ResponseData {
    pub status: u16,
    pub content_type: String,
    pub headers: Vec<(String, String)>,
    pub body: ResponseBody,
}
Expand description

The HTTP response produced by an endpoint.

Most endpoints never build one directly: the endpoint macro turns a serializable response value into application/json. Return ResponseData from a handler when you need full control over the status, headers and body — for example to stream a file:

ⓘ
async fn handle(&mut self, req: DownloadReportRequest) -> Result<ResponseData> {
    Ok(ResponseData::file(self.reports.path_for(&req.id)?)
        .content_type("text/csv")
        .download_name("report.csv"))
}

Fields§

§status: u16

HTTP status code.

§content_type: String

Media type. Applied before headers, so a header can override it.

§headers: Vec<(String, String)>

Extra response headers, applied in order.

§body: ResponseBody

The response body.

Implementations§

Source§

impl ResponseData

Source

pub fn new(status: u16) -> ResponseData

An empty 200 response.

Source

pub fn json<T>(value: &T) -> Result<ResponseData, Error>
where T: Serialize + ?Sized,

Serialize value as an application/json response.

A serialization failure here is a server-side defect in the response DTO, so it becomes Error::ResponseSerialization (500) rather than the request-parse Error::Serialization (400).

Source

pub fn no_content() -> ResponseData

204 No Content.

Source

pub fn text(body: impl Into<String>) -> ResponseData

A text/plain; charset=utf-8 response.

Source

pub fn html(body: impl Into<String>) -> ResponseData

A text/html; charset=utf-8 response.

Source

pub fn bytes(body: impl Into<Vec<u8>>) -> ResponseData

A binary response. Set ResponseData::content_type for anything other than application/octet-stream.

Source

pub fn file(path: impl Into<PathBuf>) -> ResponseData

Send the file at path — ASP.NET Core’s PhysicalFile(...).

The host opens the file once, sends Content-Length, Last-Modified and an ETag, honours conditional requests, and serves Range requests so downloads can be resumed. The media type is inferred from the extension unless overridden with ResponseData::content_type.

Source

pub fn file_stream( reader: impl AsyncRead + Send + Unpin + 'static, content_type: impl Into<String>, ) -> ResponseData

Send an async reader of unknown length — ASP.NET Core’s File(Stream, ...).

Nothing is buffered: bytes flow from the reader to the socket. The response uses chunked transfer encoding, so there is no Content-Length and no Range support; use ResponseData::file_seekable_stream when the reader knows its length and can seek.

ⓘ
// Stream a zip being built on the fly.
let (reader, writer) = tokio::io::duplex(64 * 1024);
tokio::spawn(async move { zip_into(writer).await });
Ok(ResponseData::file_stream(reader, "application/zip")
    .download_name("export.zip"))
Source

pub fn file_seekable_stream( reader: impl AsyncRead + AsyncSeek + Send + Unpin + 'static, len: u64, content_type: impl Into<String>, ) -> ResponseData

Send an async reader of known length that supports seeking.

Keeps exact Content-Length and Range/206 support, so clients can resume. Pass an crate::http::FileBody::entity_tag to also get conditional requests.

Source

pub fn with_file(file: FileBody) -> ResponseData

Send a fully specified crate::http::FileBody.

Use this to set the file-specific HTTP knobs: enable_range_processing, last_modified and entity_tag.

Source

pub fn status(self, status: u16) -> ResponseData

Override the status code.

Source

pub fn content_type(self, content_type: impl Into<String>) -> ResponseData

Override the media type.

Source

pub fn header( self, key: impl Into<String>, value: impl Into<String>, ) -> ResponseData

Add a response header.

Source

pub fn download_name(self, file_name: impl Into<String>) -> ResponseData

Send the body as a download named file_name.

For file bodies the host emits Content-Disposition while streaming; for in-memory bodies the header is set here.

Source

pub fn inline_name(self, file_name: impl Into<String>) -> ResponseData

Present the body inline under file_name.

Source

pub fn enable_range_processing(self, enabled: bool) -> ResponseData

Enable or disable Range processing for a file body.

Has no effect on in-memory bodies, which are always sent whole.

Source

pub fn last_modified(self, last_modified: SystemTime) -> ResponseData

Advertise an explicit Last-Modified.

For a file body this becomes the response validator; otherwise it is written as a plain header.

Source

pub fn entity_tag(self, entity_tag: impl Into<String>) -> ResponseData

Advertise an explicit ETag (include the quotes).

For a file body this becomes the response validator; otherwise it is written as a plain header.

Trait Implementations§

Source§

impl Debug for ResponseData

Source§

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

Formats the value using the given formatter. Read more

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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