unshiftio / url-parse

Small footprint URL parser that works seamlessly across Node.js and browser environments.
http://unshift.io
MIT License
1.03k stars 104 forks source link

unexpected encode behaviour for set query #220

Open herbertpimentel opened 2 years ago

herbertpimentel commented 2 years ago

It results unexpected encoding, depending of the type of input used on set query

var parsed = urlParse('http://go.com/');

parsed.set('query', 'promo=20%25');
console.log(parsed.href);   //outputs: "http://go.com/?promo=20%25"

parsed.set('query', {promo:'20%25'});
console.log(parsed.href);   //outputs: "http://go.com/?promo=20%2525"

https://codepen.io/herbertpimentel/pen/vYeeVdz?editors=0011

Expected result: in both cases have the same output.

lpinca commented 2 years ago

This is expected, in the first case the query string is first parsed so you get

{ promo: '20%' }

which is then stringified to 'promo=20%25'.

In the second case you are providing an already parsed query string which is only stringified.

> querystring.stringify({ promo: '20%25' })
'promo=20%2525'

to get the expected result you should use { promo: '20%' }

> querystring.stringify({ promo: '20%' })
'promo=20%25'