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

Comments

@OJezu
Copy link

@OJezu 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 mpark86 commented Dec 13, 2019

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

Loading

@chinesedfan
Copy link
Collaborator

@chinesedfan chinesedfan commented Feb 26, 2020

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

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

Loading

@OJezu
Copy link
Author

@OJezu 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.

Loading

@chinesedfan
Copy link
Collaborator

@chinesedfan chinesedfan commented Feb 26, 2020

@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.

Loading

@OJezu
Copy link
Author

@OJezu 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.

Loading

@chinesedfan
Copy link
Collaborator

@chinesedfan 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.

Loading

@OJezu
Copy link
Author

@OJezu 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

Loading

@chinesedfan
Copy link
Collaborator

@chinesedfan chinesedfan commented Feb 26, 2020

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

Loading

@alexandr2110pro
Copy link

@alexandr2110pro alexandr2110pro commented Apr 24, 2020

Any progress on that?

Loading

@OJezu
Copy link
Author

@OJezu 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.

Loading

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

@jasonsaayman jasonsaayman commented Apr 27, 2020

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

Loading

@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 added this to the v0.22.0 milestone May 15, 2021
@jasonsaayman jasonsaayman removed this from the v0.22.0 milestone 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 added this to the v1.0.0 milestone May 15, 2021
@jasonsaayman jasonsaayman removed this from the v1.0.0 milestone May 15, 2021
@jasonsaayman jasonsaayman added this to the v0.22.0 milestone 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
v0.22.0
  
Done
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants