关于Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别及用法

Posted 鱼笑笑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别及用法相关的知识,希望对你有一定的参考价值。

Form的enctype属性表示页面表单数据向服务端传输时的编码方式,常用有两种:application/x-www-form-urlencoded 和 multipart/form-data,默认为application/x-www-form-urlencoded。
1、application/x-www-form-urlencoded: 
窗体数据被编码为名称/值对,这是标准且默认的编码格式。当action为get时候,客户端把form数据转换成一个字串append到url后面,用?分割。当action为post时候,浏览器把form数据封装到http body中,然后发送到server。
application/x-www-form-urlencoded 传递时的数据构造:
......
username=twm&[email protected]
......

2、multipart/form-data:
multipart表示的意思是单个消息头包含多个消息体的解决方案。multipart媒体类型对发送非文本的各媒体类型是有用的。一般多用于文件上传。

multipart/form-data只是multipart的一种。目前常用的有以下这些类型(注:任何一种执行时无法识别的multipart子类型都被视为子类型"mixed")

 

html FORM data (see Ch. 9 and App. B) multipart/form-data
Messages with multiple parts multipart/mixed
Messages with multiple, alternative parts multipart/alternative
Message with multiple, related parts multipart/related
Multiple parts are digests multipart/digest
For reporting of email status (admin.) multipart/report
Order of parts does not matter multipart/parallel
Macintosh file data multipart/appledouble
Aggregate messages; descriptor as header multipart/header-set
Container for voice-mail multipart/voice-message
Infinite multiparts - See Chapter 9 (Netscape) multipart/x-mixed-replace

 

 

当客户端发起起求时:
请求头包含Content-Type:"multipart/form-data; boundary=---------------------------30613490016641",表明form的enctype为multipart/form-data,同时分隔每个控件field的分割符(boundary)是‘---------------------------30613490016641‘。
请求内容的每个field被分成小部分,而且包含一个value是"form-data"的"Content-Disposition"的头部;一个"name"属性对应field的ID。

multipart/form-data 传递时的数据构造,见图:

技术分享












以上是关于关于Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别及用法的主要内容,如果未能解决你的问题,请参考以下文章

HTTP Content-type 对照表

Firefox 不支持 xhrPost 中的 Content-type 标头

zabbix API基本使用方法介绍

在 Shopify 应用程序的 Django HttpResponse 对象中设置 Content-Type

关于content-type

关于 GETPOST表单Content-Type