简体   繁体   中英

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?

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());
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM