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.
I can't think of any library doing that by default, and unfortunately, I'd say your implementation may be the more efficient way to do this, since any other option would probably either do what you're already doing, or will use slower non-compiled pure JavaScript code.
What about asking the web service provider to follow the RFC?
https://github.com/kvz/phpjs is a node.js package that provides all the php functions. The http_build_query implementation at the time of writing this only supports urlencode (the query string includes + instead of spaces), but hopefully soon will include the enc_type parameter / rawurlencode (%20's for spaces).
See http://php.net/http_build_query.
RFC1738 (+'s) will be the default enc_type either way, so you can use it immediately for your purposes.