pub struct Response { /* private fields */ }Expand description
Express-style HTTP response.
Implementations§
Source§impl Response
impl Response
pub fn empty() -> Self
pub fn text(body: impl Into<String>) -> Self
pub fn html(body: impl Into<String>) -> Self
pub fn json<T: Serialize>(value: &T) -> Self
pub fn redirect(location: impl AsRef<str>) -> Self
Sourcepub fn bytes(data: impl Into<Bytes>, mime: &str) -> Self
pub fn bytes(data: impl Into<Bytes>, mime: &str) -> Self
Buffered body with an explicit MIME type.
Sourcepub fn attachment(self, filename: &str) -> Self
pub fn attachment(self, filename: &str) -> Self
Set Content-Disposition: attachment for downloads.
Sourcepub fn sse<S, E>(stream: S) -> Self
pub fn sse<S, E>(stream: S) -> Self
Server-Sent Events stream (text/event-stream).
Each item becomes one data: event (multi-line values split across data: lines).
pub fn stream(body: ResponseBody) -> Self
pub fn from_reader_stream<S>(stream: S) -> Self
pub fn header(self, name: impl AsRef<str>, value: impl AsRef<str>) -> Self
pub fn status_code(&self) -> StatusCode
pub fn headers(&self) -> &HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn take_body(&mut self) -> Body
pub fn set_body(&mut self, body: impl Into<Body>)
Sourcepub fn map_buffered_html(
&mut self,
f: impl FnOnce(&str) -> Option<String>,
) -> bool
pub fn map_buffered_html( &mut self, f: impl FnOnce(&str) -> Option<String>, ) -> bool
If this is buffered HTML, run f and replace the body when it returns Some.
Streams and non-HTML responses are left untouched. Returns whether the body changed.
Sourcepub fn body_bytes(&self) -> Option<&[u8]>
pub fn body_bytes(&self) -> Option<&[u8]>
Body bytes when the response is buffered; None for streams.
Sourcepub async fn file(path: impl AsRef<Path>) -> Self
pub async fn file(path: impl AsRef<Path>) -> Self
Stream a local file (path-traversal safe relative to its parent directory).
Sourcepub async fn file_in(dir: impl AsRef<Path>, relative: impl AsRef<Path>) -> Self
pub async fn file_in(dir: impl AsRef<Path>, relative: impl AsRef<Path>) -> Self
Stream a file under dir / relative (path-traversal safe).
Sourcepub async fn download(path: impl AsRef<Path>) -> Self
pub async fn download(path: impl AsRef<Path>) -> Self
Self::file plus Content-Disposition: attachment.
Sourcepub async fn download_in(
dir: impl AsRef<Path>,
relative: impl AsRef<Path>,
) -> Self
pub async fn download_in( dir: impl AsRef<Path>, relative: impl AsRef<Path>, ) -> Self
Self::file_in plus Content-Disposition: attachment.
Trait Implementations§
Source§impl IntoResponse for Response
impl IntoResponse for Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl !Freeze for Response
impl !RefUnwindSafe for Response
impl !UnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more