Why in some cases, API requests are accepted with fetch but not Axios

By default, Axios encodes as JSON and sets the Content-Type header automatically.
On the other side, fetch doesn’t do it automatically so it claims you are sending plain text instead of JSON.

There are no CORS requirements when you post data using text/plain format (because they are supported in forms https://www.w3.org/TR/html5/forms.html#form-submission) so when application/json is sent, the browser asks permission from the server using a CORS preflight request.
Source: https://stackoverflow.com/questions/39498731/cors-why-there-arent-a-pre-flight-request-for-post-with-content-typetext-plai

74 responses to “Why in some cases, API requests are accepted with fetch but not Axios”