1

java.net.URLEncoder will encode a single whitespace with +.

scala> val encodedSpace = URLEncoder.encode(" ", "UTF-8")
encodedSpace: String = +

However, per this W-3 doc, shouldn't it be encoded with %20?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

0

From the DOCS (http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4):

application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type must be encoded as follows:

  1. Control names and values are escaped. Space characters are replaced by +', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by %HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').

  2. The control names/values are listed in the order they appear in the document. The name is separated from the value by =' and name/value pairs are separated from each other by&'.

You have to replace it from + to %20. The prior is a default behavior.

For example:

System.out.println(java.net.URLEncoder.encode(" ", "UTF-8").replace("+", "%20"));

The above is the typical case of application/x-www-form-urlencoded.

In Summary:

multipart/form-data uses MIME encoding.

application/x-www-form-urlencoded uses +.

Properly encoded URIs use %20.

GET Submission:

http://www.bing.com/search?q=hello+world

Resource with space in the name

http://camera.phor.net/cameralife/folders/2012/2012-06%20Pool%20party/

sjain
  • 23,126
  • 28
  • 107
  • 185