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] qs.stringify does not serialise arrays in objects according to documentation #1798

Closed
3 tasks done
MaoShizhong opened this issue Apr 5, 2024 · 2 comments · Fixed by #1808
Closed
3 tasks done
Labels

Comments

@MaoShizhong
Copy link
Contributor

MaoShizhong commented Apr 5, 2024

Describe the bug

Node.js version: v20.10.0

OS version: Fedora 39 x86_64 (kernel 6.7.10-200)

Superagent version: 8.1.2

Description: According to the documentation, sending an object with an array value as x-www-urlencoded should encode via the form a=b&a=c (using repeat instead of indices when stringifying via qs).

Instead, it appears #1591 led to having both Node and the browser stringify using indices for arrays when passing such an object/array as the request body. This is an odd decision, as the same object passed as a query string results in stringifying without indices.

Either the query param behaviour and the documentation both change to match the Node and Client versions for the request body serialisation, or more preferably, the request body serialisation in both versions gets changed to match the current query param behaviour and documentation.

The below examples come from this request code:

const obj = { foo: ['bar', 'baz'] };
superagent
  .post('http://localhost:3000/test')
  .type('form')
  .send(obj)
  .query(obj)
  .end();

Actual behavior

actual behaviour

The object gets serialised using qs.stringify when sent as the request body via x-www-urlencoded. Despite the documentation saying this should serialise to foo=bar&foo=baz, it gets serialised using indices (default qs.stringify behaviour).

The same object is serialised without indices when used for query params, as it seems that is handled by a different qs.stringify call which does specify indices: false.

Expected behavior

desired behaviour

By changing the function used to serialise the request body in this scenario from

qs.stringify

to

(obj) => qs.stringify(obj, { indices: false })

the behaviour is now unified between request body and query params, and both now match the documentation.

Code to reproduce

https://github.com/MaoShizhong/superagent-qs-stringify-behaviour
Should just be able to follow the README instructions to see behaviour and apply fix (to the node_modules superagent code).

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@MaoShizhong MaoShizhong added the Bug label Apr 5, 2024
@MaoShizhong
Copy link
Contributor Author

I presume these are the lines that would need changing to make the request body serialisation match the documentation and query param serialisation for the client version and the Node version, but I am ultimately unfamiliar with this and not sure if there are further parts that would require changing in such case.

@titanism
Copy link
Collaborator

PR welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants