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) -> ActionResult<ResponseData> {
Ok(ResponseData::file(self.reports.path_for(&req.id)?)
.content_type("text/csv")
.download_name("report.csv"))
}Fields§
§status: u16HTTP status code.
content_type: StringMedia type. Applied before headers, so a header can override it.
headers: Vec<(String, String)>Extra response headers, applied in order.
body: ResponseBodyThe response body.
Implementations§
Source§impl ResponseData
impl ResponseData
Sourcepub fn new(status: u16) -> ResponseData
pub fn new(status: u16) -> ResponseData
An empty 200 response.
Sourcepub fn json<T>(value: &T) -> Result<ResponseData, Error>
pub fn json<T>(value: &T) -> Result<ResponseData, Error>
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).
Sourcepub fn no_content() -> ResponseData
pub fn no_content() -> ResponseData
204 No Content.
Sourcepub fn text(body: impl Into<String>) -> ResponseData
pub fn text(body: impl Into<String>) -> ResponseData
A text/plain; charset=utf-8 response.
Sourcepub fn html(body: impl Into<String>) -> ResponseData
pub fn html(body: impl Into<String>) -> ResponseData
A text/html; charset=utf-8 response.
Sourcepub fn bytes(body: impl Into<Vec<u8>>) -> ResponseData
pub fn bytes(body: impl Into<Vec<u8>>) -> ResponseData
A binary response. Set ResponseData::content_type for anything other
than application/octet-stream.
Sourcepub fn file(path: impl Into<PathBuf>) -> ResponseData
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.
Sourcepub fn file_stream(
reader: impl AsyncRead + Send + Unpin + 'static,
content_type: impl Into<String>,
) -> ResponseData
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"))Sourcepub fn file_seekable_stream(
reader: impl AsyncRead + AsyncSeek + Send + Unpin + 'static,
len: u64,
content_type: impl Into<String>,
) -> ResponseData
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.
Sourcepub fn with_file(file: FileBody) -> ResponseData
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.
Sourcepub fn status(self, status: u16) -> ResponseData
pub fn status(self, status: u16) -> ResponseData
Override the status code.
Sourcepub fn content_type(self, content_type: impl Into<String>) -> ResponseData
pub fn content_type(self, content_type: impl Into<String>) -> ResponseData
Override the media type.
Sourcepub fn header(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> ResponseData
pub fn header( self, key: impl Into<String>, value: impl Into<String>, ) -> ResponseData
Add a response header.
Sourcepub fn download_name(self, file_name: impl Into<String>) -> ResponseData
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.
Sourcepub fn inline_name(self, file_name: impl Into<String>) -> ResponseData
pub fn inline_name(self, file_name: impl Into<String>) -> ResponseData
Present the body inline under file_name.
Sourcepub fn enable_range_processing(self, enabled: bool) -> ResponseData
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.
Sourcepub fn last_modified(self, last_modified: SystemTime) -> ResponseData
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.
Sourcepub fn entity_tag(self, entity_tag: impl Into<String>) -> ResponseData
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.