Get custom header on proxying request to Netlify function

Hi !
Following this thread:

I tried to set up a redirection with a custom header to a Netlify function. Here is my netlify.toml conf:

[[redirects]]
  from = "/a"
  to = "https://other-host.netlify.com/.netlify/functions/slug"
  status = 200
  force = true
  headers = {X-From = "SLUG-A", X-Netlify-Hostname = "a.com"}

[[redirects]]
  from = "/b"
  to = "https://other-host.netlify.com/.netlify/functions/slug"
  status = 200
  force = true
  headers = {X-From = "SLUG-B", X-Netlify-Hostname = "b.com"}

When running the project with netlify dev, no problem, it’s working. I get the event.headers['x-netlify-hostname’] or the event.headers['x-form’] in my function handler, it’s working perfectly.

But when deployed on Netlify env, these headers are undefined.

Do you have a idea about what could be wrong with my setup ?
Thanks

Sorry it’s taken me so long to respond!

Could you tell us the actual URL you are testing so we can try to see it in place to advise? There are a few places it could be failing and I’ll need to see your site’s config as a starting place.

Hi! No problem for the delay :slight_smile:
So, here are my repos and netlify links:

1- https://github.com/kavrillon/test-netlify-functions
This project contains the Netlify function called via a Proxy redirect.

Link: https://kan-test-netlify-functions.netlify.com
Function: https://kan-test-netlify-functions.netlify.com/.netlify/functions/slug

2- https://github.com/kavrillon/test-netlify-redirect
Project which contains the Proxy Redirect. There are 4 links:

  • /a => should be a proxy to a Netlify function with a specific header
  • /b => should be a proxy to a Netlify function with another header
  • /c & /d => some tests with a link middleware

Link: https://kan-test-netlify-redirect.netlify.com

Some precisions:
when clicking on a link on this site: https://kan-test-netlify-redirect.netlify.com, some timeouts are occurring (but sometimes it works correctly).

When running this site with netlify dev, everything is working fine :thinking:
Thanks!

An other precision:
when using an other service for the proxy redirect, eg. with a PostBin call with the following call:

[[redirects]]
  from = "/a"
  to = "https://postb.in/1576576901504-5311256072018"
  status = 200
  force = true
  headers = {X-From = "SLUG-A", X-Netlify-Hostname = "a.com"}

Headers transmission is working fine. So maybe there is a problem with Functions, like there are not receiving headers correctly ?

There are only two redirects on that site, none of which you’re talking about, and none of which have any headers included:

"path"=>"/test1", "to"=>"/.netlify/functions/slug", "status"=>200, "force"=>true
"path"=>"/test2", "to"=>"/.netlify/functions/slug", "status"=>200, "force"=>true

So - I think the problem is in deploying the redirects and not in using them :slight_smile:

Those do match what you have in your toml file though: https://github.com/kavrillon/test-netlify-functions/blob/master/netlify.toml - so I think it isn’t a deployment problem either. I’m a bit confused about how you are expecting /a and /b etc to work.

Hi,
I’ve done some cleanup to clarify things. In fact, as said in my previous message there are two repos:

1- one containing the function: https://github.com/kavrillon/test-netlify-functions
Here is the link to the function: https://kan-test-netlify-functions.netlify.com/.netlify/functions/slug

2- one containing the redirects: https://github.com/kavrillon/test-netlify-redirect
Here is the link: https://kan-test-netlify-redirect.netlify.com

So, you’ll find here the redirects for /a and /b: https://github.com/kavrillon/test-netlify-redirect/blob/master/netlify.toml

Hi, any news on this problem ?
Thanks

Hi @Avri, if you deploy the functions in the same site do you have the same issue? What about if you proxy your actual api or any external api? What i’m wondering is if the issue is specifically because you’re proxying a netlify function.

Hi @futuregerald, sorry for the delay.
I updated one project to use functions in the same project than the redirect, as you can see here:
https://github.com/kavrillon/test-netlify-functions/blob/master/netlify.toml#L6

Same problem, all headers are undefined: https://kan-test-netlify-functions.netlify.com/test1

Some investigations:

  • I tried to proxy to an external service (postb.in). The headers were correctly passed.
  • I tried to test the project via netlify dev. The headers were correctly passed.

So I think your last sentence is correct: this problem seems to be be specific to netlify functions on live.
Do not hesitates if you need some extra infos.
Thanks,

@Avri, this is a bug. I was able to replicate it. I’ve also gone ahead and filed an issue for our team to look at. We’ll let you know as soon as we have more information. Sorry for the trouble!

1 Like

No problem, thanks!
Happy to contribute, let me know when you’ll have more infos.

1 Like

Actually, not confident this is a bug – see here: _headers / .toml / CORS - #7 by narrationsd

For functions, I think what’s in toml files is fully ignored – you have to code them in the function callback.

As link says.

To return headers, you do have to code them in your function’s callback or return statement. But for incoming headers as mentioned in the redirect rule, that would be a bug. Since our proxy should be adding those headers to the request so that the function sees it as part of the incoming event.

Good point though.

Sounds right, and thanks for taking the close look, Dennis – presume this will find its way into a release.

Best,
Clive

Yea, we do have an issue around that and will update here if and when we get things fixed up.

2 Likes

Having the same issue here. No luck with JSON web signatures either. I assume it’s related to the same bug. Any news?

hi there, some work has happened on this, but as it is connected to some pretty major infra changes relevant for all of the customers on our platform, we’re doing loooots of testing to make sure it all works well at scale. No specific eta yet. We’ll update here with more info once available. thanks for your patience.