user6546249
user6546249

Reputation:

Angular 5 - GET method sent as OPTIONS after adding headers

​Hello,

I have a problem with my HTTP requests. Is it possible please to help me understand what is happening?

When I am sending a GET request it is sent as OPTIONS:

let headers = new HttpHeaders();

headers = headers.set('Content-Type', 'application/json; charset=utf-8')
​.set('Authorization','bearer ' + this.auth.getUserToken());

 this.http.get(SERVER.URL_PROD + API.VERSION + API.GET_USERS, {headers: headers}).subscribe((res: any) => {
  console.log(res);
});

In my chrome dev tool debug I see the following:

Request Method: OPTIONS

Status Code: 405 Method Not Allowed

Upvotes: 0

Views: 3493

Answers (1)

Mike Tung
Mike Tung

Reputation: 4821

Like David said, you are experiencing a CORS issue. Specifically when you are on a browser, the browser sends an OPTIONS request to the api/backend to say hey can I get this before firing a GET. If the response to OPTIONS comes back as no you cannot, such as your 405 then you are prevented from doing a GET.

In order to fix this, the backend that you are hitting needs to implement CORS and allow the various HTTP METHODS that it supports.

Upvotes: 5

Related Questions