Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set http headers to requests generated by browser?

I have server with HTTP basic authentication and I have client script which knows username and password for above HTTP server. I use XHR request and it's setRequestHeader to set Authorization headers, this bit avoids browser from prompting default HTTP login dialog.

request.setRequestHeader('Authorization', authInfo);

This is normal for AJAX requests, but in case I want download file from mentioned server and I'm forced to go without AJAX and use something like window.location.href from JavaScript where browser creates request itself. Since this request will not include Authorization header (brwoser will add Authorization header for every request automatically only after it prompts login dialog and saves base64 of login info for actual server) browser will prompt HTTP login dialog, which I want to avoid.

So is there a way to set headers to non-ajax requests created by browser?

like image 229
ggat Avatar asked Jun 25 '12 07:06

ggat


1 Answers

Have you tried setting the username and password in the URL?

location.href = 'http://user:[email protected]/path/to/download';

I think that should work in most browsers except, unfortunately, Internet Explorer 7+.

Alternatively, if you have access to the server, you could put a script on the server side that uses cookies to authenticate. Cookies can be set with JavaScript.

like image 170
Nathan Wall Avatar answered Oct 22 '22 02:10

Nathan Wall