Set response headers in redirect in Netlify function

Hi @luke, @fool

I would like to set the headers when I set the return object in a netlify function.

Using…

return {
  statusCode: 302,
  headers: {
    Location: "http://localhost:8888",
    "X-Foo": "value-to-be-sent"
  }
}

Instead of using…

return {
  statusCode: 302,
  headers: {
    Location: "http://localhost:8888?X-Foo=value to be sent"
  }
}

Is there a way to do this?

:bowing_man:

Hi @berni- yes, it’s possible and you’re on the right track! Want to give something like this a shot and let us know how it goes?

return {
  statusCode: 302,
  headers: {
    Location: "http://localhost:8888",
    X-Foo: "value-to-be-sent"
  }
}

Thanks @jen!

I’ll try that now.

1 Like

Tried it and it didn’t work.

The URL making the redirection is different from the change in location.

And the http response for the URL making the redirection includes the header.

But the header doesn’t show up in the htyp response upon redirect.

hey berni, we’ll need a little more information in order to help :slight_smile:

  • you mention “the URL in redirection is different from the change in location” - I am unfortunately not sure what this means exactly. Can you share some detailed information: actual URLs, screenshots, a screen recording even?
  • the http response: we’ll need to see the HTTP response somehow in order to be able to say what is happening here. What is it?
  • if the header is not showing up, what is there instead? how are you checking?

the more details you give us the more likely we are able to help. :+1:

Hi @perry the current URL which is the endpoint processing the request is different from the URL provided in that endpoints response header location.

The http response header is:
Location: “/”
X-arg: “value”

However upon redirection to “/” the X-arg isn’t in the response header of the network request to “/”.

It is present in the response header of the current URL.

So for the time being I’ve appended the value as a query parameter upon redirection.

E.g.
Location: “/?key=value”

I would like the X-arg header value to be present in the response header of “/” when redirection takes place.

I’m not entirely sure what you are asking, so my apologies if this isn’t a sensible response. In that case please explain in more detail what you are trying to accomplish rather than how you are hoping to accomplish it and we can advise on best practices :slight_smile:

We can’t make the browser do anything on the new request (using HTTP request headers), based on your function’s HTTP response header(s). I’d be setting a cookie that you can use or something similar, in the second request.

1 Like

Hi @fool,

Looks like it can’t be done according to the HTTP protocol.

hey berni,can you explain in more detail what you are trying to say, please? We definitely need more information if you are needing more assistance. What problem are you trying to solve? Is this a non-issue? I am confused.

@perry this issue has been resolved.

It’s not actually possible.

1 Like