query-string icon indicating copy to clipboard operation
query-string copied to clipboard

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

Open ioev opened this issue 4 years ago • 4 comments

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

ioev avatar Nov 19 '21 17:11 ioev

Why would they be?

sindresorhus avatar Nov 19 '21 18:11 sindresorhus

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."

ioev avatar Nov 19 '21 18:11 ioev

i had same issue so use qs package and it works

sootood avatar Jan 22 '22 12:01 sootood

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

Reeywhaar avatar Oct 05 '23 08:10 Reeywhaar