cobolstinks
cobolstinks

Reputation: 7153

Angular Don't Send Content-Type Request Header

I have to consume an API through CORS in my Angular5 app, but this particular api isn't allowing the Content-Type header in the CORS configuration. My angular app is currently sending this header, and the preflight check is failing due to the fact that I'm sending a header that isn't explicitly allowed by the API's CORS configuration. Is there a way to remove this header for this particular request?

I was thinking along the lines of creating an interceptor but is that the most appropriate place to remove this header?

Upvotes: 2

Views: 384

Answers (1)

cobolstinks
cobolstinks

Reputation: 7153

The solution here was to not use Angular's HttpClient and use the fetch api instead. I couldn't figure out a way to use HttpClient and remove the 'Content-Type' header, even when I wrote an interceptor and explicitly removed this header it was added back somewhere.

Here is what I ended up doing:

    return fetch( environment.splunk_config.url, {
        body: JSON.stringify(_message),
        method: 'POST',
        mode: 'cors',
        headers: { 'Authorization': auth }
      }
    )
    .then(response => response.json());
  }

Upvotes: 2

Related Questions