pub struct Response { /* private fields */ }Expand description
Express-style HTTP response.
Implementations§
Source§impl Response
impl Response
pub fn empty() -> Response
pub fn text(body: impl Into<String>) -> Response
pub fn html(body: impl Into<String>) -> Response
pub fn json<T>(value: &T) -> Responsewhere
T: Serialize,
pub fn redirect(location: impl AsRef<str>) -> Response
Sourcepub fn bytes(data: impl Into<Bytes>, mime: &str) -> Response
pub fn bytes(data: impl Into<Bytes>, mime: &str) -> Response
Buffered body with an explicit MIME type.
Sourcepub fn attachment(self, filename: &str) -> Response
pub fn attachment(self, filename: &str) -> Response
Set Content-Disposition: attachment for downloads.
Sourcepub fn sse<S, E>(stream: S) -> Response
pub fn sse<S, E>(stream: S) -> Response
Server-Sent Events stream (text/event-stream).
Each item becomes one data: event (multi-line values split across data: lines).
pub fn stream(body: BoxBody<Bytes, Box<dyn Error + Send + Sync>>) -> Response
pub fn from_reader_stream<S>(stream: S) -> Response
pub fn header(self, name: impl AsRef<str>, value: impl AsRef<str>) -> Response
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>) -> Response
pub async fn file(path: impl AsRef<Path>) -> Response
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>,
) -> Response
pub async fn file_in( dir: impl AsRef<Path>, relative: impl AsRef<Path>, ) -> Response
Stream a file under dir / relative (path-traversal safe).
Sourcepub async fn download(path: impl AsRef<Path>) -> Response
pub async fn download(path: impl AsRef<Path>) -> Response
Self::file plus Content-Disposition: attachment.
Sourcepub async fn download_in(
dir: impl AsRef<Path>,
relative: impl AsRef<Path>,
) -> Response
pub async fn download_in( dir: impl AsRef<Path>, relative: impl AsRef<Path>, ) -> Response
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