Basic Authentication with Fetch

Basic authentication with fetch?

You are missing a space between Basic and the encoded username and password.

headers.set('Authorization', 'Basic ' + base64.encode(username + ":" + password));

Basic authentication using Fetch API in JS

mode: "no-cors"

Setting the mode to "no-cors" causes any attempt to do something that requires CORS permission to silently fail (instead of throwing an exception).

Setting Authorization and Content-Type headers (although setting a Content-Type on a GET request, which can't have content in the first place, doesn't make sense) requires CORS permission.


Given that the request requires an API key, and that to use it client-side you would have to distribute your API key to all your users, there is a high probability that the service is not designed to be access directly by web browsers (so you should use server-side code instead).



Related Topics



Leave a reply



Submit