1

I am trying to set headers when making a get request from Angular, but it's not working. The route is correct, I've tested it with a REST client (Insomnia) and everything is ok.

Here's my Angular code:

const options = {
  headers: {
    Authorization: 'XXXXXXXXXXXXX'
  }
};

this.http.get(this.endpoint, options).subscribe((data) => {
  console.log(data);
}, (error) => {
  console.log(error);
});

I've checked endpoint and options and I cannot see any problems. I always get error code 401 and the following message:

Access to XMLHttpRequest at XXXXXX from origin YYYYYYYYY has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I also correctly enabled CORS on API Gateway, and when I remove the Authorizer from the interface it's working correctly

Here's what I have in my request:

Chrome request status

Raphael
  • 563
  • 1
  • 10
  • 22
  • Possible duplicate of [Angular - Set headers for every request](https://stackoverflow.com/questions/34464108/angular-set-headers-for-every-request) – Bernhard Feb 01 '19 at 15:10
  • There is no problem with your Authorization header. You've got everything in error message. Its related to CORS. Check how to allow CORS origin in google. There is a lot of tutorials. – lemek Feb 01 '19 at 15:11
  • @lemek I already done that and as I said this this working from a REST client – Raphael Feb 01 '19 at 15:12
  • Use google developer tools, and check if Authorization header is properly appended to your request. Also check if your custom header does not remove anything. However its very strange behavior, and this might be not solvable problem without access to your API server. – lemek Feb 01 '19 at 15:14
  • No the Authorization header is not appended, I also tested with other headers like `Content-Type` and I have the same behavior [Screenshot](https://i.imgur.com/QEQ4UFV.png) – Raphael Feb 01 '19 at 15:21
  • @Raphael Because its only Provisional headers. I would check your CORS origin settings again. – lemek Feb 01 '19 at 15:31

0 Answers0