Struct edjx::response::HttpResponse

source ·
pub struct HttpResponse { /* private fields */ }
Expand description

An HTTP response which may include body, headers, and status code.

Each serverless function can send only one response to the client.

Sending HTTP Response to client

Normally, these methods do not need to be used explicity. EDJX sample Rust Serverless Application code file (lib.rs) uses one of these methods to process Responses and relay to the client.

If HttpResponse needs to be sent using Streams, then use HttpResponse::send_streaming().

#[no_mangle]
pub fn init() {
 let req = match HttpRequest::from_client();

 let res = crate::serverless_function::serverless(req.unwrap());
  
 match res.send() {
     Ok(x) => x,
     Err(e) => {
         error!("{}", e.to_string().as_str());
     }
 };
}

Developers need to change only the above init() function in lib.rs when the HTTP Response type needs to be modified or sent using Streams.

Construction of HTTP Responses

For interoperability with other Rust libraries, HttpResponse can be initiated using http, which is crate’s http::Response type along with the HttpResponse::send_using_standard_http_lib() function passing http::Response<Option<Bytes>> as parameters.

Builder-style Methods

HttpResponse can be used as a builder, allowing responses to be constructed and used through method chaining. Methods with the set_ name prefix, such as set_header(), return Self to allow chaining. The builder style is typically most useful when constructing and using a response in a single expression. For example:

use edjx::HttpResponse;
HttpResponse::new()
    .set_header("edjx-header".parse().unwrap(), "hello-there".parse().unwrap())
    .set_status(StatusCode::OK)
    .send();

Implementations§

Creates a client HttpResponse.

The response is created with status code 200 OK, no headers, and an empty body.

Sets the HTTP status code of the client response.

Example

Using the constants from http::StatusCode:

use edjx::HttpResponse;
use http::StatusCode;

let mut resp = HttpResponse::from("not found!").set_status(StatusCode::NOT_FOUND);
resp.send();

Returns the HTTP status code of the response.

Sets the HTTP version of this response. Use constants from http::Version:

Returns the HTTP version of this response.

Sets the given &str value as the body of the client response.

Any previous body that was set on the response is discarded.

Sets the given Vec<u8> value as the body of the client response.

Any previous body that was set on the response is discarded.

Sets a response header to the given value, discarding any previous values for the given header name.

Example
use edjx::HttpResponse;

let mut resp = HttpResponse::new().set_header("hello".parse().unwrap(), "world!".parse().unwrap());

let header_map = resp.get_headers();
assert!(header_map.contains_key("hello"));

Returns the response header map as http::header::HeaderMap.

Returns the response header map as a mutable reference to http::header::HeaderMap.

Send the response to the client.

Error Response

This method returns an error response of HttpError::Unknown if another response was sent to the client by the method HttpResponse::send_using_standard_http_lib(), or even if http connection from client has been closed.

Example
#[no_mangle]
pub fn init() {
  let req = match HttpRequest::from_client()

  let res = crate::serverless_function::serverless(req);
    match res.send() {
        Ok(x) => x,
        Err(e) => {
            error!("{}", e.to_string().as_str());
        }
    };
}

Sends the EDJX HTTP Function’s response to the client using streaming. This method returns a WriteStream, which can be used to stream data.

HttpResponse::send_streaming() and HttpResponse::send() cannot be used at the same time.

Error Response

This method returns an error response of HttpError::Unknown if another response was sent to the client by the method HttpResponse::send_using_standard_http_lib(), or even if http connection from client has been closed.

Example
use edjx::{BaseStream, HttpRequest, HttpResponse};

let mut http_resp = HttpResponse::new()
    .set_header("ServerlessStream".parse().unwrap(), "EDJX".parse().unwrap());
let mut write_stream = http_resp.send_streaming().unwrap();

write_stream.write_chunk_text("text chunk").unwrap();
write_stream.write_chunk_binary(Vec::from("binary chunk")).unwrap();
write_stream.close().unwrap();

Send the response to the client using http::Response.

Error Response

This method returns an error response of HttpError::Unknown if another response was sent.

Example

Sending a response using http::Response:

use edjx::HttpResponse;
use http::{Response, StatusCode};

let mut response = Response::builder();
response.header("Foo", "Bar").status(StatusCode::OK);
HttpResponse::send_using_standard_http_lib(response.body(()));

Trait Implementations§

Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.