What are multipart/form-data and application/x-www-form-urlencoded?

Both of them are the optional values of enctype where the <form> element specifies the content type used to encode the form data set for submission to the server. There are three choices:

  1. application/x-www-form-urlencoded (default)
  2. multipart/form-data
  3. text/plain

application/x-www-form-urlencoded

  1. Just like a querystring: name=John+Smith&age=27;
  2. Each character that cannot be expressed using the selected character encoding, replacing the character by a string like &#xxxx; where xxxx means the Unicode code point of the character in base 10. (NCR);
  3. Replace 0x20 (space) the byte by +;
  4. Leave characters match /[\*\-\.0-9A-Z_a-z]/ (alphanumeric) as is;
  5. Otherwise, replace the byte (non-alphanumeric) by %HH (must be two) where HH means the uppsercase ASCII hex digits representing the byte.

multipart/form-data

Talk is cheap, show you the code:

1
2
3
4
5
<form enctype='multipart/form-data' method='POST' action='test'>
<input type='file' name='file' value='千本桜.txt'/>
<input type='text' name='text' value='John Smith' />
<input type='submit' name='submit' value='Submit Button' />
</form>

The corresponding HTTP request would like this:

multipart/form-data

The boundary in the Content-Type works just like the & in querystring, but its value shall never appear in the entries (key and value).

The name in the Content-Disposition is the same as the entry’s name.

text/plain

Do not use.

Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).

Application

If you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data. Otherwise, use application/x-www-form-urlencoded.

BTW, if you are using ajax, there are more choices for you. Like application/json, application/xml

References

  1. application/x-www-form-urlencoded or multipart/form-data?
  2. Forms - W3C
  3. &#x开头的是什么编码呢
  4. Character Entity Reference Chart
  5. Returning Values from Forms: multipart/form-data
Comments