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

Content-type header for application/json should not contain charset field, according do RFC 8259 #2154

Closed
OJezu opened this issue May 14, 2019 · 11 comments · Fixed by #3807
Closed
Projects
Milestone

Comments

@OJezu
Copy link

OJezu commented May 14, 2019

Describe the bug
According to RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format content-type application/json does not have any charset defined, as it must be utf-8 encoded. Older specifications allowed other unicode encodings, and parsers were detecting exact encoding used by first four bytes, still not by charset param.

Axios defaults to header Content-Type: application/json;charset=utf-8 for some types of sent data.

This breaks some HTTP servers and applications, who are not recognizing the content-type.

To Reproduce

axios.post("http://example.com", {an: "object"});
// Content-Type: application/json;charset=utf-8 header will be set

Expected behavior
Content-Type: application/json header should be set as default for object data, without charset parameter.

Environment:

  • Axios Version - 0.18.0, I've also checked master branch source - it is still there.
    setContentTypeIfUnset(headers, 'application/json;charset=utf-8');

Additional context/Screenshots
This breaks some parts of the rabbitmq-management API.

Related issues:
#859

@OJezu OJezu added the bug label May 14, 2019
@mpark86
Copy link
Contributor

mpark86 commented Dec 13, 2019

Is there any update on this issue? I'd like to try creating a PR for this.

@chinesedfan
Copy link
Collaborator

See #2603 (comment). Thanks for @mpark86' pull request anyway.

And if you don't like it, you can replace it easily by configuring transformRequest.

@OJezu
Copy link
Author

OJezu commented Feb 26, 2020

@chinesedfan just Ctrl+F the RFC referenced in that comment and in this issue.

Note: No "charset" parameter is defined for this registration.
Adding one really has no effect on compliant recipients.

o Added a note to the "IANA Considerations" clarifying the absence
of a "charset" parameter for the application/json media type.

@chinesedfan
Copy link
Collaborator

@OJezu I am not sure I get what you means. I guess you are proving your point. According to RFC 8259, the charset is not required and some servers are not supported to parse it.

Never mind, at least MDN includes the charset. More servers work well with it. And as I said, if you don't like it, you can replace it easily by configuring transformRequest. Thanks for understanding.

@OJezu
Copy link
Author

OJezu commented Feb 26, 2020

@chinesedfan what I'm proving, is that application/json type does not have "charset" defined. Providing it is non-standard, no matter what the servers do with it.

Axios is behaving in a way not specified in standard, and this is what the issue is about. Common errors alone in other software are not a reason to break RFC.

@chinesedfan
Copy link
Collaborator

chinesedfan commented Feb 26, 2020

@OJezu After read more carefully, I think they are 2 things.

  • You mentioned RFC 8259, which requires JSON should be UTF-8 encoded. And media type should be application/json.
  • And I pasted a MDN link, which includes RFC 7231.

Content-Type is defined in RFC 7231, instead of RFC 8259. And its format is media-type charset boundary. So axios doesn't break any BFCs.

@OJezu
Copy link
Author

OJezu commented Feb 26, 2020

@chinesedfan where in RFC7231 is charset parameter defined for application/json? It's not, because parameter names are defined for concrete media-types in appropriate RFCs. charset parameter is neither declared for all headers nor for all media-types. application/json media-type does not have "charset" parameter defined.

From RFC7231

The presence or absence of a
parameter might be significant to the processing of a media-type,
depending on its definition within the media type registry.

See IANA media type registry for application/json:
https://www.iana.org/assignments/media-types/application/json

And compare to plain/html entry which has charset parameter explicitly defined:
https://www.iana.org/assignments/media-types/text/html

@chinesedfan
Copy link
Collaborator

@OJezu I see. Now you have persuaded me. application/json is different with other media types.

@alexandr2110pro
Copy link

Any progress on that?

@OJezu
Copy link
Author

OJezu commented Apr 27, 2020

There was a pull request done #2603, but @mpark86 did not change the expected header in tests, so they are failing. Until somebody fixes tests I expect no progress. I switched to node-fetch, so I lost interest anyway. If you can't switch out, tough luck, I suggest fixing the PR and hoping someone will merge and release.

@jasonsaayman jasonsaayman added this to To do in v0.20.0 via automation Apr 27, 2020
@jasonsaayman
Copy link
Member

We should try mend this in 0.20.0 I will look at the pull request.

@jasonsaayman jasonsaayman removed this from To do in v0.20.0 Aug 25, 2020
@jasonsaayman jasonsaayman added this to To do in v0.22.0 via automation May 15, 2021
@jasonsaayman jasonsaayman modified the milestone: v0.22.0 May 15, 2021
@jasonsaayman jasonsaayman removed this from To do in v0.22.0 May 15, 2021
@jasonsaayman jasonsaayman added this to To do in v0.22.0 via automation May 15, 2021
@jasonsaayman jasonsaayman modified the milestones: v1.0.0, v0.22.0 May 15, 2021
v0.22.0 automation moved this from To do to Done May 21, 2021
kr8n3r added a commit to alphagov/paas-admin that referenced this issue Sep 14, 2021
In axios release v0.21.2 they have [removed the charset field from application/json Content-type header](axios/axios#2154) which UAA api doesn't like so paas-admin password reset requests have been 404ing for users.

This adds the charset back so all is good now.
ballpointcarrot pushed a commit to ballpointcarrot/aws4-axios that referenced this issue Jan 3, 2022
axios v0.21.2 [removes the charset from JSON Content-Type
headers](axios/axios#2154). This adjusts the
tests to compensate for that change.
jamesmbourne added a commit to jamesmbourne/aws4-axios that referenced this issue Feb 4, 2022
* chore(deps-dev): bump axios from 0.21.1 to 0.24.0

Bumps [axios](https://github.com/axios/axios) from 0.21.1 to 0.24.0.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/master/CHANGELOG.md)
- [Commits](axios/axios@v0.21.1...v0.24.0)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: remove charset from tests

axios v0.21.2 [removes the charset from JSON Content-Type
headers](axios/axios#2154). This adjusts the
tests to compensate for that change.

* test: change axios mocking

use 'axios-mock-adapter' over 'moxios' due to library changes that cause
incompatibilites with new versions of axios (over 0.21).

* build: update axios to 0.24

* fix: upgrade to axios 0.25.0

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: James Bourne <jamesmbourne@users.noreply.github.com>
Co-authored-by: James Bourne <jamesbourne@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v0.22.0
  
Done
Development

Successfully merging a pull request may close this issue.

5 participants