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

0.21.4 incorrectly stringifies null data #4099

Closed
jhpedemonte opened this issue Sep 28, 2021 · 2 comments
Closed

0.21.4 incorrectly stringifies null data #4099

jhpedemonte opened this issue Sep 28, 2021 · 2 comments

Comments

@jhpedemonte
Copy link

Describe the bug

We're using apisauce, but used yarn's resolutions to update axios to latest version, to pick up security fix. But that broke our app.

We're making a call to post() setting data as undefined (apisauce changes that to null) and we have headers set to Content-Type: "application/json" (data isn't always null/undefined; most times we're sending JSON data). This has worked fine until upgrading axios to 0.21.4 -- now, the payload for is actually null instead of no payload.

This is do to this change from PR #3688: it blindly stringifies data with content type is "application/json".

This was not an issue with 0.21.1.

To Reproduce

Call post() with data set to null. Notice that actual XHR payload is string "null".

Expected behavior

XHR has no payload

Environment

  • Axios Version 0.21.4
  • Adapter [e.g. XHR/HTTP]
  • Browser Chrome
  • Browser Version 93.0.4577.82
  • Node.js Version [e.g. 13.0.1]
  • OS: MacOS 11.6
  • Additional Library Versions: apisauce 2.1.1 (latest)

Additional context/Screenshots

Add any other context about the problem here. If applicable, add screenshots to help explain.

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Sep 28, 2021

it blindly stringifies data with content type is "application/json".

Actually, it does exactly what you ask it to do when you set contentType to application/json - it passes the data as JSON. Null is a valid JSON payload. As discussed earlier, this is expected RFC-compliant behavior. Passing null as a no payload sign is erroneous with application/json, so this is an apisauce issue.

According to json.org, a valid JSON payload is one of these seven values:

object => { example: true }
array => [ 42, 43 ]
string => "foo"
number => -42
"true" => true
"false" => false
"null" => null

@jhpedemonte
Copy link
Author

Yep, you are correct. I'll push on apisauce to update. Thanks.

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

2 participants