tags:

views:

1514

answers:

2

When is a space in a URL encoded to + and when is it encoded to %20?

+5  A: 

From Wikipedia (emphasis mine):

When data that has been entered into HTML forms is submitted, the form field names and values are encoded and sent to the server in an HTTP request message using method GET or POST, or, historically, via email. The encoding used by default is based on a very early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with "+" instead of "%20". The MIME type of data encoded this way is application/x-www-form-urlencoded, and it is currently defined (still in a very outdated manner) in the HTML and XForms specifications.

So, the real percent encoding uses %20 while form data in URLs is in a modified form that uses +. So you're most likely to only see + in URLs in the query string after an ?.

Joey
So + encoding would technically be multipart/form-data encoding, while percent encoding is application/x-www-form-urlencoded?
BC
@BC: no - `multipart/form-data` uses MIME encoding; `application/x-www-form-urlencoded` uses `+` and properly encoded URIs use `%20`.
McDowell
+1  A: 

I would recommend %20.

Are you hard-coding them?

This is not very consistent across languages, though. If I'm not mistaken, in PHP urlencode() treats spaces as + whereas Python's urlencode() treats them as %20.

Rui Vieira
Not hardcoding. Trying to determine from an aesthetic perspective what my urls containing spaces will look like.
BC
PHP also has `rawurlencode()` which uses `%20`.
eyelidlessness