pub struct HttpResponse<Body = BoxBody> { /* private fields */ }Expand description
The HTTP response struct
Implementations§
Source§impl HttpResponse<BoxBody>
impl HttpResponse<BoxBody>
Sourcepub fn new<T: TryInto<StatusCode>>(status_code: T) -> Self
pub fn new<T: TryInto<StatusCode>>(status_code: T) -> Self
Create a new instance of the HttpResponse struct with a specific status code.
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Get the status code of the response
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Get the mutable headers of the response
Sourcepub fn body<B>(self, body: B) -> Selfwhere
B: MessageBody + Clone + 'static,
pub fn body<B>(self, body: B) -> Selfwhere
B: MessageBody + Clone + 'static,
Sets the body for the response.
The provided body must implement both MessageBody and Clone, and it must have a 'static lifetime to be compatible.
§Note
Sending tuples as the body is supported, but it’s important to note that the layout of tuple elements may be unpredictable when serialized to bytes, as Rust does not guarantee element ordering in tuples.
§Parameters
body: The body content to be set for the response, implementingMessageBodyandClone.
§Returns
Returns Self, allowing for method chaining.
§Example
let response = HttpResponse::new(200)
.body("Hello, world!");Sourcepub fn Ok() -> Self
pub fn Ok() -> Self
Creates a new HttpResponse with a status code of 200 (OK).
Examples found in repository?
examples/device_id.rs (line 107)
78async fn devices() -> HttpResponse {
79 let devices = [
80 Device {
81 id: 1,
82 mac: String::from("5F-33-CC-1F-43-82"),
83 firmware: String::from("2.1.6"),
84 },
85 Device {
86 id: 2,
87 mac: String::from("EF-2B-C4-F5-D6-34"),
88 firmware: String::from("2.1.5"),
89 },
90 Device {
91 id: 3,
92 mac: String::from("62-46-13-B7-B3-A1"),
93 firmware: String::from("3.0.0"),
94 },
95 Device {
96 id: 4,
97 mac: String::from("96-A8-DE-5B-77-14"),
98 firmware: String::from("1.0.1"),
99 },
100 Device {
101 id: 5,
102 mac: String::from("7E-3B-62-A6-09-12"),
103 firmware: String::from("3.5.6"),
104 },
105 ];
106
107 HttpResponse::Ok().json(&devices)
108}Sourcepub fn json<B>(self, data: &B) -> Selfwhere
B: Serialize,
pub fn json<B>(self, data: &B) -> Selfwhere
B: Serialize,
Creates a new HttpResponse with a status code of 201 (Created).
Examples found in repository?
examples/device_id.rs (line 107)
78async fn devices() -> HttpResponse {
79 let devices = [
80 Device {
81 id: 1,
82 mac: String::from("5F-33-CC-1F-43-82"),
83 firmware: String::from("2.1.6"),
84 },
85 Device {
86 id: 2,
87 mac: String::from("EF-2B-C4-F5-D6-34"),
88 firmware: String::from("2.1.5"),
89 },
90 Device {
91 id: 3,
92 mac: String::from("62-46-13-B7-B3-A1"),
93 firmware: String::from("3.0.0"),
94 },
95 Device {
96 id: 4,
97 mac: String::from("96-A8-DE-5B-77-14"),
98 firmware: String::from("1.0.1"),
99 },
100 Device {
101 id: 5,
102 mac: String::from("7E-3B-62-A6-09-12"),
103 firmware: String::from("3.5.6"),
104 },
105 ];
106
107 HttpResponse::Ok().json(&devices)
108}Trait Implementations§
Source§impl<Body: Clone> Clone for HttpResponse<Body>
impl<Body: Clone> Clone for HttpResponse<Body>
Source§fn clone(&self) -> HttpResponse<Body>
fn clone(&self) -> HttpResponse<Body>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<Body: Debug> Debug for HttpResponse<Body>
impl<Body: Debug> Debug for HttpResponse<Body>
Source§impl From<HttpResponse> for Response<BoxBody>
impl From<HttpResponse> for Response<BoxBody>
Source§fn from(value: HttpResponse<BoxBody>) -> Self
fn from(value: HttpResponse<BoxBody>) -> Self
Converts to this type from the input type.
Source§impl Responder for HttpResponse<BoxBody>
impl Responder for HttpResponse<BoxBody>
Source§fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>
Returns the response for the request
Auto Trait Implementations§
impl<Body> Freeze for HttpResponse<Body>where
Body: Freeze,
impl<Body> RefUnwindSafe for HttpResponse<Body>where
Body: RefUnwindSafe,
impl<Body> Send for HttpResponse<Body>where
Body: Send,
impl<Body> Sync for HttpResponse<Body>where
Body: Sync,
impl<Body> Unpin for HttpResponse<Body>where
Body: Unpin,
impl<Body> UnwindSafe for HttpResponse<Body>where
Body: UnwindSafe,
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