Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaults headers Content-Type don't allow "content-type" ? #3104

Closed
KiKiKi-KiKi opened this issue Jul 6, 2020 · 5 comments
Closed

Defaults headers Content-Type don't allow "content-type" ? #3104

KiKiKi-KiKi opened this issue Jul 6, 2020 · 5 comments

Comments

@KiKiKi-KiKi
Copy link

Describe the issue

axios.defaults.headers.common['content-type'] = 'application/json';

This request header Content-Type become application/json, application/json;charset=utf-8.
GET / POST methods option can use 'content-type' too.
But defaults.headers is only allow the 'Content-Type' ?

Example Code

Global axios defaults

axios.defaults.headers.common['Content-Type'] = 'application/json';
// Request Header Content-Type: application/json

axios.defaults.headers.common['content-type'] = 'application/json';
// Request Header Content-Type: application/json, application/json;charset=utf-8

axios.defaults.headers.common['Content-type'] = 'application/json';
// Request Header Content-Type: application/json, application/json;charset=utf-8

Request methods config

axios.get(url, { headers: { 'Content-Type': 'application/json' } });
// Request Header Content-Type: application/json

axios.get(url, { headers: { 'content-type': 'application/json' } });
// Request Header Content-Type: application/json

axios.get(url, { headers: { 'Content-type': 'application/json' } });
// Request Header Content-Type: application/json

axios.get(url, { headers: { 'cOntEnt-tYpe': 'application/json' } });
// Request Header Content-Type: application/json

Content-Type application/json, application/json;charset=utf-8 requests ite become empty req.body in Express.

// Express API
const express = require('express');
const app = express();
app.use(express.json());

app.post('/api', (req, res) => {
  // Always `req.body` become `{}` 
  // When Content-Type is "application/json, application/json;charset=utf-8"
  req.body;
});

Expected behavior, if applicable

Request methods config can use Content-Type and content-type too. (This document was written the "content-type" in Response Schema )
But defaults.headers can use only Content-Type.
It's made me little confused. I sorry, I don't know if it's a specification or not.
if it's a specification, need a note of this on the "Global axios defaults" section, I think. Thank you.

Environment

  • Axios Version [0.19.2]
  • Adapter [XHR/HTTP]
  • Browser [Chrome]
  • Browser Version [83.0.4103.116]
  • Node.js Version [v12.16.3]
  • OS: [OSX 10.15.5]
  • Additional Library Versions [React 16.13.1 / Express 4.17.1]

Additional context/Screenshots

@MoonSupport
Copy link

MoonSupport commented Jul 7, 2020

I think defaults.headers is only allow the 'Content-Type'.

For expected work, it has to call normalizeHeaderName function https://github.com/axios/axios/blob/405fe690f93264d591b7a64d006314e2222c8727/lib/helpers/normalizeHeaderName.js

But If you will directly change header like this

axios.defaults.headers.common['Content-Type'] = 'application/json';

There is nowhere to call this function.

But I agree your opinion need a note of this on the "Global axios defaults" section.

@karandeepvirk85
Copy link

karandeepvirk85 commented Jul 8, 2020

I had this same issue couple of days ago. I had resolved this issue by using this header.

{ 'content-type': 'application/x-www-form-urlencoded' }
However I got data as POST in the API. I think it first encode URL with data to Axios module then Axios Post to API.

Here are screenshots
Sending Request
Untitled
Browser
Untitled2

@MoonSupport
Copy link

MoonSupport commented Jul 9, 2020

@karandeepvirk85
Post api config call normalizeHeaderName.
Also Content-type Setting in Axios Module has a higher priority than user setting ( in post api or default )

@chinesedfan
Copy link
Collaborator

But defaults.headers is only allow the 'Content-Type' ?

See #2880.

This document was written the "content-type" in Response Schema

Note that it explains headers in response, which is different with axios config. And I'd like share more-stories-about-headers.

@MoonSupport
Copy link

MoonSupport commented Jul 19, 2020

Note that it explains headers in response, which is different with axios config. And I'd like share more-stories-about-headers.

After looking at your document, I think my explanation is wrong.

Now The headers setting is not case sensitive.
But regardless of the above the Content-Type cannot be set of it.

Is it right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants