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

Axios 0.21.1 post request ignores false as a request payload #3549

Closed
anagligoric opened this issue Jan 14, 2021 · 17 comments
Closed

Axios 0.21.1 post request ignores false as a request payload #3549

anagligoric opened this issue Jan 14, 2021 · 17 comments
Assignees
Milestone

Comments

@anagligoric
Copy link

Describe the bug

After updating Axios from 0.19.2 to 0.21.1, post request ignores false (boolean value) when passed as a request payload, but true is sent properly

To Reproduce

axios.post(url, false,
            {
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(() => {
                ....
            })
            .catch(() => ....);

Expected behavior

The false value shouldn't be ignored.

Environment

  • Axios Version 0.21.1
  • Adapter HTTP
  • Browser Chrome, Safari
  • Browser Version Chrome - Version 87.0.4280.88, Safari - Version 14.0.2
  • Node.js Version v12.18.0
  • OS: OSX 11.1
  • Additional Library Versions React 16.13.1

Additional context/Screenshots

Converting false boolean value to string works properly, but some servers cannot deserialize string "false" to boolean false value [e.g .NET Core].

@ChickenProp
Copy link

Looks like this was introduced in #1487. Should the check be if (requestData === undefined || requestData === '') instead?

@jsubloom
Copy link

I had the same issue. In addition to false (boolean), it repros for the number 0 as well. Unsurprising given the changelist ChickenProp pointed to.

@jasonsaayman
Copy link
Member

Hi @anagligoric

Are you intending to send a post request with the body purely being a boolean value?

Thanks

@ChickenProp
Copy link

I can't speak for them, but that's what I was intending to do when I encountered this bug.

@Calamari
Copy link

We have the same problem with sending 0 as payload in a put call. I used to work in 0.19, but is not working with 0.21 anymore.

@jasonsaayman
Copy link
Member

Hi All,

Ok well, this is not exactly how the docs show Axios should be used as the second param of the post operation within the documentation shows an object must be passed. I know this worked previously but completely un-intentionally and Axios matures I am certain we will not be adding back what was pretty much an oversight.

Thanks

@GLObus303
Copy link

GLObus303 commented Feb 3, 2021

Hey @jasonsaayman ,

sending values as a body of a request seems legit JSON object - https://tools.ietf.org/html/rfc7158#page-5. The same goes for the standard of REST API.

At this moment, we are basically stuck on 0.19.0 version where this is "supported"

@ChickenProp
Copy link

Clarification: the docs actually say that

  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer

(This is easy to miss, and the typescript type is more general than that, but it is there.)

So calling JSON.stringify yourself on bools and numbers should work.

@GLObus303
Copy link

Even this works as it is cast to string basically and passes the if (!requestData) condition. It still doesn't solve the root of the problem.

@Calamari
Copy link

Calamari commented Feb 4, 2021

I tend to agree with @GLObus303, since I also think that valid JSON objects should be accepted as payload, but if you still decide to keep that "new" behavior of only allowing objects and string, then please can someone adapt the typing of those post/patch/put methods to incorporate this decision?
Currently, they say data: any which clearly is not the case (anymore). It should be object | string, so that at least TypeScript can catch that problem early on.

@jasonsaayman
Copy link
Member

Hi

Ok cool well yeah, I agree with you that the JSON spec is pretty clear as for the REST API standard well that is maybe a bit less unclear since it is pretty difficult to lock down official documentation. However, that being said I believe that we should allow a JSON body and in saying that we would need to support the spec for a valid JSON object. So I will start work on fixing this issue, I will probably start on this tomorrow.

Thanks

@jasonsaayman jasonsaayman reopened this Feb 4, 2021
@jasonsaayman jasonsaayman self-assigned this Feb 4, 2021
@jasonsaayman jasonsaayman added this to the v0.22.0 milestone Feb 4, 2021
@jasonsaayman jasonsaayman added this to To do in v0.22.0 Feb 16, 2021
@jasonsaayman jasonsaayman moved this from To do to Done in v0.22.0 Feb 16, 2021
@jasonsaayman jasonsaayman removed this from Done in v0.22.0 Feb 16, 2021
@GersonDias
Copy link

could this be related to #3526 ?

@marcacyr
Copy link

marcacyr commented Apr 1, 2021

When is 0.22.0 being released with this fix by chance?

@jasonsaayman
Copy link
Member

Hi

I would like to get 0.22 completed by mid to end April, there are some other changes that will need to be in there including new build tools for axios etc.

Thanks

@marcacyr
Copy link

marcacyr commented Apr 1, 2021

Thank you @jasonsaayman!

@Mehdi-HAFID
Copy link

should not this issue be closed???

@mehdicharife
Copy link

It's been 3 years now. Will there be a fix anytime soon?

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

11 participants