ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.47k stars 730 forks source link

How can you keep the square brackets when using arrayFormat: comma #481

Open samgermain opened 11 months ago

samgermain commented 11 months ago

With the following code

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'

plus some option, I want to obtain a result that looks like

// 'a=[b,c]'

How can I do this?

ljharb commented 11 months ago

That’s not the comma format, so I’m not sure how you can.

What server expects that format??

samgermain commented 11 months ago

That’s not the comma format, so I’m not sure how you can.

well it doesn't need to be the comma format then, but it needs to be that format

What server expects that format??

binance

ljharb commented 11 months ago

oof, so binance basically just invented their own format instead of using the universal ones that already exist?

I'm not sure how to do that with qs; perhaps with a custom encoder?

lzdyes commented 9 months ago

You can use filter.

qs.stringify(params, {
  filter: (prefix, value) => {
    if (Array.isArray(value)) return `[${value.join(',')}]`
    return value
  },
})