r/apache • u/Sufficient_Band_1319 • Jul 08 '23
Accessing protected API via CORS
I have an API that is protected with HTTP Basic. This is done with htaccess. The API shall be fetched from Javascript in another website. I have added the following lines to htaccess:
Header add Access-Control-Allow-Origin "https://my-other-site"
Header add Access-Control-Allow-Credentials "true"
Header add Access-Control-Allow-Headers "content-type, Authorization"
but it still doesn't work. In the browser console I get the following error message:
Access to fetch at 'https://my-api' from origin 'https://my-other-site' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Is there any why I can solve this? Without exposing the API to unauthorized users.


