qs icon indicating copy to clipboard operation
qs copied to clipboard

Stringifying array escape brackets

Open jagwingchoy opened this issue 4 years ago • 1 comments

Hi there! I am using this package for stringifying data before sending requests to the backend. I recently have a case like below:

let body = { pets: ["has_dog", "has_cat"] };
config.body = qs.stringify(body, { arrayFormat: 'brackets' });
console.log("config body", config.body);

Expected Result

pets[]=has_dog&pets[]=has_cat

Current Result

pets%5B%5D=has_dog&pets%5B%5D=has_cat

Does anyone facing same issue? How you can solve it so that brackets will not be escaped? Thanks.

jagwingchoy avatar Dec 28 '20 03:12 jagwingchoy

Try:

qs.stringify(body, { arrayFormat: 'brackets', encodeValuesOnly: true });

ljharb avatar Dec 28 '20 03:12 ljharb