Search code examples
javascriptbasic-authentication

Set headers using javascript


I need to download a file from a web service which uses basic authentication. This means I need to add an authorization header to the request. I know how to do this using jQuery Ajax, but I CANNOT use Ajax since I need to download a file from the url and Ajax requests cannot download files to disk.

How can I set the authorization header before I do window.location = url ?

I searched the web, but everything I find is about jQuery Ajax which I cannot use.


Solution

  • You can put them in the URL:

    https://user:password@host/path?query
    

    Note that browsers may block this for security reasons.
    (These URLs are frequently used with usernames like google.com for phishing attacks)