Skip to content

Accessing RequestContext #443

Closed
Closed

Description

@niroam

Hi,

A question not an issue - still learning rust as well so bear with me :)

could someone help understand how the Object models work with the request types

I am calling my handler function like this

lambda_http::run(service_fn(|event: Request| process_request(event))).await?;

so passing in an event object of type Request, the event is coming from an APIGW Lambda Proxy Integration

within the handler if i look at the request context using event.request_context() -> which returns the object type RequestContext

debug!("TEST### {:?}", event.request_context());

i can see this

ApiGatewayV1("ApiGatewayProxyRequestContext"{
   "account_id":"Some(""1231321232123"")",
   "resource_id":"Some(""xxxxx"")",
   "operation_name":"None",
   "stage":"Some(""dev"")",
   "domain_name":"Some(""11111111111.amazonaws.com"")",
   "domain_prefix":"Some(""dddddd"")",
   "request_id":"Some(""xxxxxxxxxx"")",
   "protocol":"Some(""HTTP/1.1"")",
   "identity":"ApiGatewayRequestIdentity"{
      "cognito_identity_pool_id":"None",
      "account_id":"None",
      "cognito_identity_id":"None",
      "caller":"None",
      "api_key":"None",
      "api_key_id":"None",
      "access_key":"None",
      "source_ip":"Some(""1111111111"")",
      "cognito_authentication_type":"None",
      "cognito_authentication_provider":"None",
      "user_arn":"None",
      "user_agent":"Some(""PostmanRuntime/7.28.4"")",
      "user":"None"
   },
   "resource_path":"Some(""/ledger/{id}"")",
   "authorizer":{
      "accesskey":"String(""hhhhhhhh"")",
      "secretkey":"String(""lllllll"")",
      "tenantId":"String(""1234567xyz"")",
      "integrationLatency":Number(370),
      "principalId":"String(""User|bbbb.am"")",
      "sessiontoken":"String(""hhhhhhhhh"")",
      "userName":"String(""niro.am"")"
   },
   "http_method":"GET",
   "request_time":"Some(""09/Mar/2022:20:20:45 +0000"")",
   "request_time_epoch":1646857245706,
   "apiid":"Some(""hhhhhhhh"")"
})

Can anyone point me in the right direction on how you access the data inside the ApiGatewayProxyRequestContext object ? The generic RequestContext object itself does not seem to have any way to get the data ?

Activity

  1. nmoutschen commented on Mar 10, 2022

    @nmoutschen
    Contributor

    Hey @niroam !

    The RequestContext is an enum, and the actual data could vary based on the source of the call (API Gateway REST, HTTP, or WebSockets APIs, or an ALB). To access the inner data, you'll need to either use if let or match. If you know for sure which variant it will be and don't plan on changing it, I'd recommend using if let.

    E.g. with if let:

    let req_ctx = request.request_context();
    
    if let RequestContext::ApiGatewayV1(inner_ctx) = req_ctx {
        // Do something with the request context
    }

    E.g. with match:

    let req_ctx = request.request_context();
    
    match req_ctx {
        RequestContext::ApiGatewayV1(inner_ctx) => {
            // Do something with the request context
        },
        RequestContext::ApiGatewayV2(inner_ctx) => {
            // Do something with the request context if you ever decide to switch to HTTP API
        },
        _ => {
            // Default case
            panic!("unsupported API type")
        },
    }
  2. locked and limited conversation to collaborators on Mar 10, 2022
  3. converted this issue into a discussion #444 on Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions