Query string white space vs plus

Trivia: What is the difference between the encoded query string parameter “a+b” and “a%20b” ?

Answer: Nothing! They are both encoded representations for “a b”.

Isn’t a “+” supposed to remain a “+”? Well, the URL and the query strings are not encoded following the same rules. In the URL, the “+” remains a “+” indeed, but in the query string it’s actually encoded and becomes a “%2B”. This can be misleading.

So basically, the choice is left to the encoder. In Python we get

In [1]: from urllib import urlencode
In [2]: urlencode({"a" : "a b+c"})
Out[2]: 'a=a+b%2Bc'

And in JavaScript

"a=" + encodeURIComponent("a b+c");

So both converted the “+” as a “%2B” and Python encoded the white space as “+” while JavaScript encoded it as “%20”.

Remember, the encoding differs between the URL and the query strings so the functions called do too. In python, to encode a URL you do

In [1]: from urllib import quote
In [2]: quote('test.com/a b+c')
Out[2]: 'test.com/a%20b%2Bc'

And in JavaScript you do

encodeURI('test.com/a b+c')

Again, the results differ but are both valid. Python and JavaScript both encoded the white space as “%20” while the “+” remained the same in JavaScript but was converted to “%2B” in Python.

Mind blown.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.