Struct edjx::request::HttpRequest[][src]

pub struct HttpRequest { /* fields omitted */ }
Expand description

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

Implementations

Returns the client request being handled by the serverless function.

Prefetch Body

By passing the prefetch_body flag as true, the whole request body will be read from request at this function call time and then reside in the function memory.

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(true);
  crate::serverless_function::serverless(req.unwrap());
}

init() function in lib.rs needs to be changed only if the HTTP body prefetch needs to be disabled or 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>.

Prefetch Body

If the HTTP request body was not prefetched, the same will be read on this method invocation.

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

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

Performs the conversion.

Performs the conversion.

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.