qs.parse doesn't handle encoded comma when parsing array with comma option
See original GitHub issueHello this is my first time opening an issue here! First of all thank you for this amazing library.
Basically my company is following jsonapi’s spec for formatting filter in URL
GET /comments?filter[post]=1,2 HTTP/1.1
This works fine if the comma is not encoded
it('should parse post into an array', () => {
expect(
qs.parse('filter[post]=1,2', {
comma: true,
})
).toStrictEqual({ filter: { post: ['1', '2'] } })
})
Atm our http library automatically encodes comma, which I suspect makes parse treat the comma-separated list as just a string.
it('should parse post into an array', () => {
expect(
qs.parse('filter[post]=1%2C2', {
comma: true,
})
).toStrictEqual({ filter: { post: ['1', '2'] } })
})
This test fails and the returned array is a string, "1,2"
.
Please let me know if there’s anything that I miss that would help.
P/S: After reading the source code and the tests I think maybe this feature is not supported.
t.test('arrayFormat: comma allows only comma-separated arrays', function (st) {
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' });
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
st.end();
});
If you think this is not a bug but rather a missing feature, do you think it should be included in the lib?
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Parse query string arrays using either multi-key or comma syntax
I have a .NET Core 6.0 Azure Function HTTP Trigger. I would it like to handle an array query ...
Read more >qs - npm
A querystring parser that supports nesting and arrays, with a depth limit. ... (Note: the encoder option does not apply if encode is...
Read more >qs - UNPKG
The CDN for qs. ... 36, - [Fix] parses comma delimited array while having percent-encoded ... 174, - [New] `qs.stringify`: add `encodeValuesOnly` option....
Read more >parse_str - Manual - PHP
All variables created (or values returned into array if second parameter is set) are ... As an alternative, I use a "proper" querystring...
Read more >node_modules/qs · 9bb473c15fffcd9e809a5cfebc14fa7993f6a88e ...
When using the plainObjects option the parsed value is returned as a null object, ... Some people use comma to join array, qs...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve made a branch with 5 failing tests - 2 of which I don’t actually know how it would be possible to represent (nested arrays inside an object using the comma format).
A PR into that branch that improves the tests and/or provides a solution would be much appreciated.
I don’t think #237 is the solution to the problem above. I agree, that my comment is a duplicate of #387. I only commented in this issue, because it seemed relevant to me at the time.