8

Is it possible for requests to the API-Gateway to pass the referrer URL to Lambda? For example, I'd love to let my lambda functions know if a request comes from the domain "good.com" vs. "bad.com".

I'm familiar with the list of supported $context Variables and I know a referrer url is not in there. I'm wondering if there is another way. If it is possible, what steps do I need to take?

2 Answers 2

6

Here's how to do it.

  1. As it turns out, the mapping template allows you to map HTTP headers, not just the list of supported variables in the documentation.

  2. The HTTP header that contains the referrer domain is called "Origin". The header that contains the referer page URL is called "Referer".

  3. So, for example, you can put this in your mapping template and it will grab the associated header information:

    {
    "origin" : "$input.params('origin')",
    "referer" : "$input.params('referer')"
    }
    

Origin grabs example.com. Referer grabs example.com/pagename

Sign up to request clarification or add additional context in comments.

4 Comments

It's returning null values for origin and referer headers. Although "$input.params('something')" works for any other HTTP-custom header. Any tip?
@FedericoklezCulloca Have u happened to see cases where there is no origin and no referer even though the requests are most probably invoked from a website?
@Jun I only fixed a missing quote in the answer. You should ask the user who wrote the answer.
@user3303554 Have u happened to see cases where there is no origin and no referer even though the requests are most probably invoked from a website? thanks
1

It's an HTTP header, so if you are mapping HTTP headers in the template it will be passed to the Lambda function. Look at this answer for an example of how to map HTTP headers in the request template.

1 Comment

Thanks for the tip. It helped me answer question and I shared a specific answer below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.