Struct edjx::request::HttpRequest
source · pub struct HttpRequest { /* private fields */ }
Expand description
Request which may include version, body, headers, method, and URL.
Implementations§
source§impl HttpRequest
impl HttpRequest
sourcepub fn from_client() -> Result<HttpRequest, HttpError>
pub fn from_client() -> Result<HttpRequest, HttpError>
Returns the client request being handled by the serverless function.
Panics
This method panics if the client request has already been retrieved by this method once and the body has been fetched.
Limits Exceeded Errors
If the request exceeds the limits specified, this method sends an error response with a corresponding error code for the exceeding limit, so that the same can be handled explicitly by returning a customized error page.
Normally, this method does not need to be used explicity. EDJX sample
Rust Serverless Application
code file (lib.rs) uses this method to fetch HTTP Request.
lib.rs
:
#[no_mangle]
pub fn init() {
let req = HttpRequest::from_client();
crate::serverless_function::serverless(req.unwrap());
}
init()
function in lib.rs
needs to be changed only if a
mutable reference to the request is required.
sourcepub fn method(&self) -> &HttpMethod
pub fn method(&self) -> &HttpMethod
Returns the enum value for HttpMethod.
Example
use std::str::FromStr;
fn serverless(req: &HttpRequest) {
let method = req.method();
assert_eq!(method, &HttpMethod::from_str("get").unwrap());
}
sourcepub fn query_param_by_name(&self, name: &str) -> Option<String>
pub fn query_param_by_name(&self, name: &str) -> Option<String>
Returns the optional value for a provided key in query paramters.
sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
Returns the map of headers in the request as an http::HeaderMap
object.
Use different functions available in http::HeaderMap
for the corresponding
object, as needed.
Example
use edjx::{HttpRequest};
fn serverless(req: &HttpRequest) {
let headers = req.headers();
assert!(headers.get("host").is_some());
assert_eq!(headers.get("host").unwrap(), &"example.com");
}
sourcepub fn body(&mut self) -> Result<&Option<Vec<u8>>, HttpError>
pub fn body(&mut self) -> Result<&Option<Vec<u8>>, HttpError>
Returns the body associated with the request as a Result
response of
Vec<u8>
.
Example
use edjx::{error, HttpRequest, HttpResponse};
fn serverless(req: &HttpRequest) {
let body = match req.body() {
Ok(body_vec_option) => body_vec_option,
Err(e) => {
error!("{}", &e.to_string());
HttpResponse::new()
.set_status(StatusCode::BAD_REQUEST)
.send()
.unwrap();
return;
}
}
}
sourcepub fn get_read_stream(&mut self) -> Result<&mut ReadStream, HttpError>
pub fn get_read_stream(&mut self) -> Result<&mut ReadStream, HttpError>
Opens a read stream to read the request body.
HttpRequest::body
and HttpRequest::get_read_stream
cannot be used at the same time.