Daniel Sperry
Daniel Sperry

Reputation: 4491

How to get the original request URI in api gateway?

From a lambda implemented api gateway resource, how to get the original request URI. Or even just the original path?

Lacking a better way I'm currently using the following three variables that I pass down to the lambda using the default request template:

$context.resourcePath   contains the path with variable names   ex: "/blah/{var}"
$input.params().path    contains the variable names and values  ex: {"var":"something"}
$context.stage          contains the stage                      ex: "prod"

That's quite a hassle since it requires path variable substitution to get the original call path:

/prod/blah/something

How can I get the original URL or URI?

Upvotes: 7

Views: 1983

Answers (2)

mvanbaak
mvanbaak

Reputation: 1

I found a 'workaround'.

If you create a custom domain name with a BasePathMapping, and call the API using this custom domain, the original request uri actually has your stage name in there:

Call directly to the API gateway:

curl -v 'https://some-id.execute-api.eu-central-1.amazonaws.com/v1/ping'
...
request.url: https://some-id.execute-api.eu-central-1.amazonaws.com/ping'

But if we call it through te custom domain (which is actually a cloudfront distribution):

curl -v -X GET https://api.our.domain.name.com/v1/ping
...
request.url: https://api.our.domain.name.com/v1/ping

In my opinion, the direct call gives you an INCORRECT request url in the lambda function, as the url very clearly has the stage name in there.

This breaks the routing middleware of at least flask.

Any update on the feature request?

Upvotes: 0

MikeD at AWS
MikeD at AWS

Reputation: 3745

I'm not finding anything in the documentation that lets you get the original call URI. I can add a feature request to consider adding it. Can you describe your use case. Why do you want to get the original URI?

Upvotes: 3

Related Questions