7

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?

user3303554
  • 2,215
  • 2
  • 16
  • 16

2 Answers2

5

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

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
user3303554
  • 2,215
  • 2
  • 16
  • 16
  • It's returning null values for origin and referer headers. Although "$input.params('something')" works for any other HTTP-custom header. Any tip? – bazaglia Apr 28 '17 at 22:07
  • @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 Nov 09 '18 at 18:21
  • @Jun I only fixed a missing quote in the answer. You should ask the user who wrote the answer. – Federico klez Culloca Nov 10 '18 at 01:04
  • @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 – Jun Nov 10 '18 at 02:05
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.

Community
  • 1
  • 1
Mark B
  • 183,023
  • 24
  • 297
  • 295