pub trait RequestExt {
    // Required methods
    fn raw_http_path(&self) -> String;
    fn with_raw_http_path(self, path: &str) -> Self;
    fn query_string_parameters(&self) -> QueryMap;
    fn with_query_string_parameters<Q>(self, parameters: Q) -> Self
       where Q: Into<QueryMap>;
    fn path_parameters(&self) -> QueryMap;
    fn with_path_parameters<P>(self, parameters: P) -> Self
       where P: Into<QueryMap>;
    fn stage_variables(&self) -> QueryMap;
    fn request_context(&self) -> RequestContext;
    fn with_request_context(self, context: RequestContext) -> Self;
    fn payload<D>(&self) -> Result<Option<D>, PayloadError>
       where D: for<'de> Deserialize<'de>;
    fn lambda_context(&self) -> Context;
    fn with_lambda_context(self, context: Context) -> Self;
}
Expand description

Extentions for lambda_http::Request structs that provide access to API gateway and ALB features.

Examples

A request’s body can be deserialized if its correctly encoded as per
the request’s Content-Type header. The two supported content types are application/x-www-form-urlencoded and application/json.

The following handler will work an http request body of x=1&y=2 as well as {"x":1, "y":2} respectively.

use lambda_http::{service_fn, Error, Context, Body, IntoResponse, Request, Response, RequestExt};
use serde::Deserialize;

#[derive(Debug,Deserialize,Default)]
struct Args {
  #[serde(default)]
  x: usize,
  #[serde(default)]
  y: usize
}

#[tokio::main]
async fn main() -> Result<(), Error> {
  lambda_http::run(service_fn(add)).await?;
  Ok(())
}

async fn add(
  request: Request
) -> Result<Response<Body>, Error> {
  let args: Args = request.payload()
    .unwrap_or_else(|_parse_err| None)
    .unwrap_or_default();
  Ok(
     Response::new(
       format!(
         "{} + {} = {}",
         args.x,
         args.y,
         args.x + args.y
       ).into()
     )
  )
}

Required Methods§

source

fn raw_http_path(&self) -> String

Return the raw http path for a request without any stage information.

source

fn with_raw_http_path(self, path: &str) -> Self

Configures instance with the raw http path.

source

fn query_string_parameters(&self) -> QueryMap

Return pre-parsed http query string parameters, parameters provided after the ? portion of a url, associated with the API gateway request.

The yielded value represents both single and multi-valued parameters alike. When multiple query string parameters with the same name are expected, query_string_parameters().get_all("many") to retrieve them all.

No query parameters will yield an empty QueryMap.

source

fn with_query_string_parameters<Q>(self, parameters: Q) -> Selfwhere Q: Into<QueryMap>,

Configures instance with query string parameters

This is intended for use in mock testing contexts.

source

fn path_parameters(&self) -> QueryMap

Return pre-extracted path parameters, parameter provided in url placeholders /foo/{bar}/baz/{boom}, associated with the API gateway request. No path parameters will yield an empty QueryMap

These will always be empty for ALB triggered requests

source

fn with_path_parameters<P>(self, parameters: P) -> Selfwhere P: Into<QueryMap>,

Configures instance with path parameters

This is intended for use in mock testing contexts.

source

fn stage_variables(&self) -> QueryMap

Return stage variables associated with the API gateway request. No stage parameters will yield an empty QueryMap

These will always be empty for ALB triggered requests

source

fn request_context(&self) -> RequestContext

Return request context data associated with the ALB or API gateway request

source

fn with_request_context(self, context: RequestContext) -> Self

Configures instance with request context

This is intended for use in mock testing contexts.

source

fn payload<D>(&self) -> Result<Option<D>, PayloadError>where D: for<'de> Deserialize<'de>,

Return the Result of a payload parsed into a serde Deserializeable type

Currently only application/x-www-form-urlencoded and application/json flavors of content type are supported

A PayloadError will be returned for undeserializable payloads. If no body is provided, Ok(None) will be returned.

source

fn lambda_context(&self) -> Context

Return the Lambda function context associated with the request

source

fn with_lambda_context(self, context: Context) -> Self

Configures instance with lambda context

Implementations on Foreign Types§

source§

impl RequestExt for Request<Body>

source§

fn raw_http_path(&self) -> String

source§

fn with_raw_http_path(self, path: &str) -> Request<Body>

source§

fn query_string_parameters(&self) -> QueryMap

source§

fn with_query_string_parameters<Q>(self, parameters: Q) -> Request<Body>where Q: Into<QueryMap>,

source§

fn path_parameters(&self) -> QueryMap

source§

fn with_path_parameters<P>(self, parameters: P) -> Request<Body>where P: Into<QueryMap>,

source§

fn stage_variables(&self) -> QueryMap

source§

fn request_context(&self) -> RequestContext

source§

fn with_request_context(self, context: RequestContext) -> Request<Body>

source§

fn lambda_context(&self) -> Context

source§

fn with_lambda_context(self, context: Context) -> Request<Body>

source§

fn payload<D>(&self) -> Result<Option<D>, PayloadError>where D: for<'de> Deserialize<'de>,

Implementors§