Accessing RequestContext #444
-
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
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
i can see this
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 ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @niroam ! The E.g. with let req_ctx = request.request_context();
if let RequestContext::ApiGatewayV1(inner_ctx) = req_ctx {
// Do something with the request context
} E.g. with 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")
},
} |
Beta Was this translation helpful? Give feedback.
-
Thankyou @nmoutschen! sorry for the noob question -> that makes perfect sense now I understand this bit from the RequestContext code better!
|
Beta Was this translation helpful? Give feedback.
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 useif let
ormatch
. If you know for sure which variant it will be and don't plan on changing it, I'd recommend usingif let
.E.g. with
if let
:E.g. with
match
: