HttpResponse

Struct HttpResponse 

Source
pub struct HttpResponse<Body = BoxBody> { /* private fields */ }
Expand description

The HTTP response struct

Implementations§

Source§

impl HttpResponse<BoxBody>

Source

pub fn new<T: TryInto<StatusCode>>(status_code: T) -> Self
where T::Error: Debug,

Create a new instance of the HttpResponse struct with a specific status code.

Examples found in repository?
examples/basic.rs (line 93)
88async fn not_working(
89    path: HttpPath<(u8, String)>,
90) -> Result<impl Responder<Body = BoxBody>, HttpServerError> {
91    let (id, name) = path.into_inner();
92
93    Ok(HttpResponse::new(200).body((id.to_string(), name)))
94}
Source

pub fn status_code(&self) -> StatusCode

Get the status code of the response

Source

pub fn headers(&self) -> &HeaderMap

Get the headers of the response

Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Get the mutable headers of the response

Source

pub fn set_body(self, body: BoxBody) -> Self

Set the body of the response

Source

pub fn body<B>(self, body: B) -> Self
where 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, implementing MessageBody and Clone.
§Returns

Returns Self, allowing for method chaining.

§Example

let response = HttpResponse::new(200)
    .body("Hello, world!");
Examples found in repository?
examples/basic.rs (line 93)
88async fn not_working(
89    path: HttpPath<(u8, String)>,
90) -> Result<impl Responder<Body = BoxBody>, HttpServerError> {
91    let (id, name) = path.into_inner();
92
93    Ok(HttpResponse::new(200).body((id.to_string(), name)))
94}
Source

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}
Source

pub fn json<B>(self, data: &B) -> Self
where 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>

Source§

fn clone(&self) -> HttpResponse<Body>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Body: Debug> Debug for HttpResponse<Body>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<HttpResponse> for Response<BoxBody>

Source§

fn from(value: HttpResponse<BoxBody>) -> Self

Converts to this type from the input type.
Source§

impl Responder for HttpResponse<BoxBody>

Source§

type Body = BoxBody

The body of the response
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more