4
querystring.stringify({
    error: 'error_status'
})

querystring is deprecated, how would I replace it with the native URLSearchParams here?

1 Answer 1

8
const params = new URLSearchParams({
  error: 'error_status'
});

console.log(params.toString());
// error=error_status

console.log(`?${params.toString()}`);
// ?error=error_status

More information


Here's another example demonstrating how array values are handled:

let arrayParams = new URLSearchParams({
    category: ['cat', 'dog']
});

arrayParams.append('tag', 'cat');
arrayParams.append('tag', 'dog');

console.log(arrayParams.toString());
// category=cat%2Cdog&tag=cat&tag=dog

console.log(decodeURIComponent('cat%2Cdog'))
// cat,dog
Sign up to request clarification or add additional context in comments.

1 Comment

This may result different outputs in case of passing arrays as params. querysyring.stringify({key: ['value1', 'value2']}) will be key=value1&key=value2, where as new URLSearchParams({key: ['value1', 'value2']}).toString() will be key=value1%2Cvalue2 where %2C is encoded ,. For array parameters you can refer this solution: stackoverflow.com/questions/38797509/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.