Don't encode urls in querystring stringify

Is there any option for qs.stringify that will not encode the urls?

$ node
> var qs = require("querystring");
undefined
> qs.stringify({"url": "http://domain.com"});
'url=http%3A%2F%2Fdomain.com'

I want the folowing output:

'url=http://domain.com'

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It’s a little late, but for the next person, you can do:
querystring.unescape(myString)

Solution 2

Late answer again, but…
qs.stringify() has option encode:false which actually disable the URI encoding.

Qs.stringify documentation

You can also use it in nodejs request/request module as:

request({
  url: 'http://url.domain'
  qs: {
    qs1: 'thisIsNotEncodedInTheRequest%20://асд'
  },
  qsStringifyOptions: {
    // encoding: false /** (OLD VERSION - I think is deprecated yet) */
    encode: false
  }
});

Solution 3

Not directly, no. Although, if you are not escaping the value in the query string then there is hardly any benefit to using querystring at all. Mind as well just do: var q = 'url=http://domain.com'

EDIT: From looking at the source, the only way would be to change the behavior of (i.e. overwrite) the querystring escape() function – which is possible but not a good idea.

Solution 4

Here is the answer:

qs.stringify({url: "http://domain.com"}, { encodeURIComponent: uri => uri });

The option “encodeURIComponent: uri => uri” is to disable the encoding.

Solution 5

This worked for me

qs.stringify({url: "http://example.com"}, null, null, { encodeURIComponent: qs.unescape });

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply