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

Fix #4440, #3549 - Request ignores false, 0 and '' as body values #4782

Merged
merged 1 commit into from
Jun 14, 2022

Conversation

visortelle
Copy link
Contributor

@visortelle visortelle commented Jun 12, 2022

Axios is frequently used to make requests to REST APIs that are under client's control.

There are APIs that expect boolean values like false, or number values 0 as both are valid JSON values.

I see no reason why it shouldn't work. If there are, please point me to it.

If the authors will approve this PR, I can make the same changes to 0.x and 1.x branches.

Thank you.


Related issues:

@visortelle visortelle changed the title Fix #4440 - Request ignores false and 0 body values Fix #4440 - Request ignores false, 0 and '' as body values Jun 12, 2022
@visortelle
Copy link
Contributor Author

visortelle commented Jun 13, 2022

CI failed with the error unrelated to PR.

Run actions/setup-node@v2
Found in cache @ /opt/hostedtoolcache/node/14.19.3/x64
/opt/hostedtoolcache/node/14.1[9](https://github.com/axios/axios/runs/6856026000?check_suite_focus=true#step:3:10).3/x64/bin/npm config get cache
/home/runner/.npm
Error: getCacheEntry failed: Cache service responded with 503

Maybe rerun can help?

@visortelle
Copy link
Contributor Author

Ping @jasonsaayman

@visortelle visortelle changed the title Fix #4440 - Request ignores false, 0 and '' as body values Fix #4440, #3549 - Request ignores false, 0 and '' as body values Jun 13, 2022
visortelle added a commit to tealtools/pulsar-admin-client-js that referenced this pull request Jun 13, 2022
visortelle added a commit to tealtools/pulsar-admin-client-js that referenced this pull request Jun 13, 2022
@jasonsaayman
Copy link
Member

Cool passes CI merging

@jasonsaayman jasonsaayman merged commit c343807 into axios:v2.x Jun 14, 2022
@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Jun 15, 2022

Hmm. This PR does not seem to fix anything - Axios will send valid JSON values with a valid content-type header (application/json) rather than the default application/x-www-form-urlencoded. All value conversions are done in the default request resolver/transformer to match between http and xhr adapters.
Even if Axios didn't support this, this behavior shouldn't be hard-coded inside the adapter, especially without considering the content-type header.
After conversions, the xhr adapter expected to get a payload of one of these types, supported by XMLHttpRequest:

void send();
void send(ArrayBuffer data);
void send(ArrayBufferView data);
void send(Blob data);
void send(Document data);
void send(DOMString? data);
void send(FormData data);

Calls: send(), send(null) and send('') are equal, so simple if(!requestData) was a valid condition.

In addition, the PR does not handle these values in the right way:

  • '' (empty string) will be sent as an empty response, as it was before. The JSON string primitive requires quotes ("") for an [empty] string, but not every empty string needs to be encoded as a JSON string only if content-type is set to application/json;
  • null will be handled as an empty response too (as it was)

@jasonsaayman maybe this needs to be reviewed again?

@jasonsaayman
Copy link
Member

Yeah, I am going to rever this as I should have looked into it a bit more. Thanks for the catch.

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

Successfully merging this pull request may close these issues.

None yet

3 participants