Struct edjx::request::HttpRequest

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

Request which may include version, body, headers, method, and URL.

Implementations§

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.

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());
}

Returns the request URI value as http::Uri object. Use different functions available in http::Uri for the corresponding object, as needed.

Example
use edjx::{HttpRequest};

fn serverless(req: &HttpRequest) {
    let uri = req.uri();
    assert_eq!(uri.path(), "/foo/bar");
}

Returns the query string of request as an Option of &str.

Returns the optional value for a provided key in query paramters.

Returns the path part of the request URI.

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");
}

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

Opens a read stream to read the request body.

HttpRequest::body and HttpRequest::get_read_stream cannot be used at the same time.

Trait Implementations§

Formats the value using the given formatter. Read more

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.