3

Is there any way to add a custom HTTP header in a HTTP redirect triggered by javascript?

I'm looking for something that would add a customer header element to any redirect.

There is a way to do it with AJAX requests:
How can I add a custom HTTP header to ajax request with js or jQuery?

Community
  • 1
  • 1
Jared Teng
  • 311
  • 1
  • 5
  • 21
  • `header in HTTP before a redirect` ? – Cerlin Jan 08 '16 at 07:00
  • Custom header element in the HTTP request header, sorry my bad. – Jared Teng Jan 08 '16 at 07:02
  • "customer header" is not "custom header". And what kind of redirect are you talking about, that could be controlled from clientside? There is nothing in "HTML, Javascript, & JQuery" world that could be called a redirect, except the refresh meta tag, I suppose. – Amadan Jan 08 '16 at 07:03
  • Depends on the server – Jaromanda X Jan 08 '16 at 07:03
  • @Jaromanda, From the client side. – Jared Teng Jan 08 '16 at 07:04
  • @Amadan, sorry, please read the question again. :) – Jared Teng Jan 08 '16 at 07:04
  • sending request with custom request depends on using what you are sending request. – Cerlin Jan 08 '16 at 07:04
  • @Cerlin, What do you mean? Say a redirect from a button. That points to a server controller/handler. Just plain HTML. We got Ajax requests covered. Can you give examples on, "depends on using what you are sending request" to better understand which ones are you specifying. – Jared Teng Jan 08 '16 at 07:06
  • @JaredT: for browser redirect, you cannot set custom headers. atleast AFAIK – Cerlin Jan 08 '16 at 07:07
  • @Cerlin, oh dang. Not even a Javascript/Jquery event handler that senses it? :\ – Jared Teng Jan 08 '16 at 07:08
  • no. browser doesnt allow javascript to have any control over redirects. If you want to pass data to server in a `GET` redirect request then you should use cookies or url params. – Cerlin Jan 08 '16 at 07:10
  • actually, you possibly can do it, for xmlhttprequest/fetch calls, but it involves creating an addon (so specific code for each browser) - you can "intercept" the network flow and do funky stuff ... but it's very advanced stuff – Jaromanda X Jan 08 '16 at 07:39
  • Do you mean redirecting with `window.location = newurl;`? – Barmar Jan 08 '16 at 07:52
  • @Barmar. Yes. Also these kinds, – Jared Teng Jan 08 '16 at 08:08
  • 1
    I don't think there's any way to control the headers that get sent. Those are treated just like typing the URL into the browser's address bar. If you want control over HTTP data, you have to use `XMLHttpRequest` – Barmar Jan 08 '16 at 08:12
  • 1
    Use AJAX (with custom headers) to get the new page, replace `document.firstElement.innerHTML`, use `pushState` to change the displayed URL. It just might be possible; not sure if it's worth the effort :P – Amadan Jan 08 '16 at 08:51

1 Answers1

10

I suppose you mean redirecting by window.location.

It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. In other words, there's no way to trigger an HTTP redirect and cause the client to add a custom header.

The only way for a site to instruct a browser to issue an HTTP request with a custom header is to use the XMLHttpRequest object. And it needs CORS implemented on the target server to allow such ajax requests.

The closest workaround you can come up with would be to act as a proxy. e.g., Send ajax requests with custom HTTP headers set, retrieve the response and inject it into the DOM which is a case for building upon the wrong idea.

sepehr
  • 17,110
  • 7
  • 81
  • 119