pub struct Response {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
/* private fields */
}Expand description
HTTP Response wrapper
Fields§
§status: StatusCode§headers: HeaderMap§body: BytesImplementations§
Source§impl Response
impl Response
pub fn new(status: StatusCode) -> Self
Sourcepub fn stream() -> (BodySender, Self)
pub fn stream() -> (BodySender, Self)
A response whose body is produced while it is being sent
Returns the sender for the body chunks and the response; set status and
headers on the response as usual. The body ends when the last
BodySender is dropped, and send fails
with Disconnected once the client is gone.
Middleware that rewrites body (compression, ETags) leaves streams alone.
use ullun::prelude::*;
async fn countdown(_req: Request) -> Result<Response> {
let (tx, res) = Response::stream();
tokio::spawn(async move {
for n in (1..=3).rev() {
if tx.send(format!("{n}\n")).await.is_err() {
return; // client went away
}
}
});
Ok(res.with_header("content-type", "text/plain"))
}Sourcepub fn is_streaming(&self) -> bool
pub fn is_streaming(&self) -> bool
Whether the body is streamed instead of held in body
Set a cookie
pub fn ok() -> Self
pub fn with_status(self, status: StatusCode) -> Self
pub fn with_body(self, body: impl Into<Bytes>) -> Self
pub fn with_header( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
pub fn json<T: Serialize>(data: T) -> Self
pub fn text(text: impl Into<String>) -> Self
Sourcepub fn redirect(location: impl Into<String>) -> Self
pub fn redirect(location: impl Into<String>) -> Self
303 See Other, the right answer after a form POST (Post/Redirect/Get)
Sourcepub fn redirect_permanent(location: impl Into<String>) -> Self
pub fn redirect_permanent(location: impl Into<String>) -> Self
308 Permanent Redirect, keeps method and body
pub fn from_error(error: Error) -> Self
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
impl UnwindSafe 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