Search code examples
node.jsquery-stringurlencodeurl-encoding

Node.js URL-encoding for pre-RFC3986 urls (using + vs %20)


Within Node.js, I am using querystring.stringify() to encode an object into a query string for usage in a URL. Values that have spaces are encoded as %20.

I'm working with a particularly finicky web service that will only accept spaces encoded as +, as used to be commonly done prior to RFC3986.

Is there a way to set an option for querystring so that it encodes spaces as +?

Currently I am simply doing a .replace() to replace all instances of %20 with +, but this is a bit tedious if there is an option I can set ahead of time.


Solution

  • If anyone still facing this issue, "qs" npm package has feature to encode spaces as +

    qs.stringify({ a: 'b c' }, { format : 'RFC1738' })