Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom header in HTTP before redirect

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?

like image 393
Jared Teng Avatar asked Mar 08 '26 09:03

Jared Teng


1 Answers

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.

like image 77
sepehr Avatar answered Mar 11 '26 03:03

sepehr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!