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§
source§impl HttpResponse
impl HttpResponse
sourcepub fn new() -> HttpResponse
pub fn new() -> HttpResponse
Creates a client HttpResponse
.
The response is created with status code 200 OK
, no headers, and an empty body.
sourcepub fn set_status(self, status: StatusCode) -> Self
pub fn set_status(self, status: StatusCode) -> Self
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();
sourcepub fn get_status(&self) -> StatusCode
pub fn get_status(&self) -> StatusCode
Returns the HTTP status code of the response.
sourcepub fn set_version(self, version: Version) -> Self
pub fn set_version(self, version: Version) -> Self
Sets the HTTP version of this response. Use constants from http::Version
:
sourcepub fn get_version(&self) -> Version
pub fn get_version(&self) -> Version
Returns the HTTP version of this response.
sourcepub fn set_text(self, text: &str) -> Self
pub fn set_text(self, text: &str) -> Self
Sets the given &str
value as the body of the client response.
Any previous body that was set on the response is discarded.
sourcepub fn set_vec_bytes(self, bytes: Vec<u8>) -> Self
pub fn set_vec_bytes(self, bytes: Vec<u8>) -> Self
Sets the given Vec<u8>
value as the body of the client response.
Any previous body that was set on the response is discarded.
sourcepub fn set_header(self, header_name: HeaderName, value: HeaderValue) -> Self
pub fn set_header(self, header_name: HeaderName, value: HeaderValue) -> Self
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"));
sourcepub fn get_headers(&self) -> &HeaderMap
pub fn get_headers(&self) -> &HeaderMap
Returns the response header map as http::header::HeaderMap
.
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns the response header map as a mutable reference to http::header::HeaderMap
.
sourcepub fn send(&self) -> Result<(), HttpError>
pub fn send(&self) -> Result<(), HttpError>
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());
}
};
}
sourcepub fn send_streaming(&mut self) -> Result<WriteStream, HttpError>
pub fn send_streaming(&mut self) -> Result<WriteStream, HttpError>
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();
sourcepub fn send_using_standard_http_lib(
res: Response<Option<Bytes>>
) -> Result<(), HttpError>
pub fn send_using_standard_http_lib(
res: Response<Option<Bytes>>
) -> Result<(), HttpError>
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(()));