Form表单编码格式(转)

原文链接:https://blog.****.net/yangguosb/article/details/83662910

Form表单是HTML的标签,包含了很多组件,比如input、单选框和复选框等等;

背景介绍

  HTTP协议规定了POST提交的数据都必须在消息体Entity中,但是并没有明确规定Entity的编码格式,而是提供了一种灵活的方式:在请求头Content-Type中指定Entity的编码格式,接收方根据Content-Type解析消息体的内容;

编码格式

  Form表单提供了三种编码方式,用于对表单数据的编码,介绍如下。

application/x-www-form-urlencoded

  表单数据的格式与URL的查询字符串格式一样:key1=val1&key2=val2且被URL编码,如下所示:
Form表单编码格式(转)

multipart/form-data

  产生背景:在发送大量二进制数据或者包含非ASCII字符的文本时,application/x-www-form-urlencoded编码方式并不高效,因此multipart/form-data编码方式产生。
(https://tools.ietf.org/html/rfc2046#section-5.1);

  The encoding type application/x-www-form-urlencoded is inefficient for sending large quantities of binary data or text containing non-ASCII characters. Thus, a new media type, multipart/form-data, is proposed as a way of efficiently sending the values associated with a filled-out form from client to server.

  multipart含义:消息体的数据由多部分Part组成,Part间通过boundary分隔符分隔,每个Part包含元信息和数据两部分,具体见[rfc2046]

  具体格式:表单数据以表单控件为单位被分隔符boundary分隔成多个part,每个part包含content-disposition(form-data)、Content-Type(可选,默认为text/plain)以及name(控件名称的信息),如下图所示:
Form表单编码格式(转)

参考:

  1. 基于表单的文件上传:http://www.ietf.org/rfc/rfc1867.txt;
  2. multipart/form-data 文档:https://tools.ietf.org/html/rfc7578;
  3. multipart含义:https://tools.ietf.org/html/rfc2046#section-5.1;
  4. Content-type对照表:http://tool.oschina.net/commons?type=22013-05-1