- Published on
TIL: Why does Axios is ignoring Content-Type header?
- Authors
- Name
- Ashik Nesin
- @AshikNesin
TLDR
If the payload is empty. Then axios explictly ignores the 'Content-Type' even if we set it while making the request or an interceptor/default.
And the story behind it
When playing around with Spring Boot REST API, I tried to integrate it with boilerplate that we're building at work.
As expected, I ran into CORS issue.
After fixing it, I thought everything should work as expected however I've started getting 405 error instead.
I just quickly went through the UI codebase and saw we were setting Content-Type as application/json but for some reason, it was getting ignored.
Tried to set common "Content-Type" as application/json using interceptor. No luck.
So what was the issue?
Apparently, I was wrongly setting the payload for POST
request and it was empty when making the request. And axios is explictly ignores Content-Type
if the payload is empty.