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: 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 json<T: Serialize + ?Sized>(value: &T) -> Result<Self>
pub fn json<T: Serialize + ?Sized>(value: &T) -> Result<Self>
Serialize value as an application/json response.
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
204 No Content.
Sourcepub fn bytes(body: impl Into<Vec<u8>>) -> Self
pub fn bytes(body: impl Into<Vec<u8>>) -> Self
A binary response. Set ResponseData::content_type for anything other
than application/octet-stream.
Sourcepub fn file(path: impl Into<PathBuf>) -> Self
pub fn file(path: impl Into<PathBuf>) -> Self
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>,
) -> Self
pub fn file_stream( reader: impl AsyncRead + Send + Unpin + 'static, content_type: impl Into<String>, ) -> Self
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>,
) -> Self
pub fn file_seekable_stream( reader: impl AsyncRead + AsyncSeek + Send + Unpin + 'static, len: u64, content_type: impl Into<String>, ) -> Self
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) -> Self
pub fn with_file(file: FileBody) -> Self
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 content_type(self, content_type: impl Into<String>) -> Self
pub fn content_type(self, content_type: impl Into<String>) -> Self
Override the media type.
Sourcepub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn header(self, key: impl Into<String>, value: impl Into<String>) -> Self
Add a response header.
Sourcepub fn download_name(self, file_name: impl Into<String>) -> Self
pub fn download_name(self, file_name: impl Into<String>) -> Self
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>) -> Self
pub fn inline_name(self, file_name: impl Into<String>) -> Self
Present the body inline under file_name.
Sourcepub fn enable_range_processing(self, enabled: bool) -> Self
pub fn enable_range_processing(self, enabled: bool) -> Self
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) -> Self
pub fn last_modified(self, last_modified: SystemTime) -> Self
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>) -> Self
pub fn entity_tag(self, entity_tag: impl Into<String>) -> Self
Advertise an explicit ETag (include the quotes).
For a file body this becomes the response validator; otherwise it is written as a plain header.