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

stringify with arrayFormat: 'brackets' doesn't urlEncode the square brackets #333

Open
ioev opened this issue Nov 19, 2021 · 4 comments
Open

Comments

@ioev
Copy link

ioev commented Nov 19, 2021

It seems keys generated from arrays with arrayFormat: 'bracket' don't get urlEncoded:

stringify({ a: [1, 2, 3 ]}, { arrayFormat: 'bracket' }); => a[]=1&a[]=2&a[]=3

@sindresorhus
Copy link
Owner

Why would they be?

@ioev
Copy link
Author

ioev commented Nov 19, 2021

From what I understood, they are not supposed to be in the URI unencoded?
https://newbedev.com/is-array-syntax-using-square-brackets-in-url-query-strings-valid

"This is why, for example, it is not a violation of RFC 3986 when PHP accepts both unencoded and percent encoded square brackets as valid characters in a query string, and even assigns to them a special purpose. However, it would appear that authors who try to take advantage of this loophole by not percent encoding square brackets are in violation of RFC 3986."

@sootood
Copy link

sootood commented Jan 22, 2022

i had same issue so use qs package and it works

@Reeywhaar
Copy link

Stringifying query parameter which includes brackets gives inconsistent result which mixes encoded and unencoded, so at least it better be consistent at least

const q = qs.stringify({ 'filter[product]': 'test', 'filter[id]': [1, 2, 3] }, { arrayFormat: 'bracket' })
console.log(q) //  filter%5Bid%5D[]=1&filter%5Bid%5D[]=2&filter%5Bid%5D[]=3&filter%5Bproduct%5D=test

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

4 participants