multipart/form-data和application/x-www-form-urlencoded区别
FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型。
例如:
application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。
multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。
text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符
FORM的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded,但在向服务器发送大量的文本、包含非ASCII字段的文本或者二进制数据时,application/x-www-form-urlencoded编码方式效率较低(每个非字母数字字符都将由一个% 和代表字符编码的两个十六进制数代替,这增加了传输非字母数字字符的数据量),所以在文件上载时,所使用的编码类型应当是“multipart/form-data”,它告诉我们传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,multipart/form-data既可以发送文本数据,也可以支持二进制数据上传。
当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url;
当action为post时候,浏览器把form数据封装到http body中,名称/值对之间用&分隔,然后发送到server。 如果没有type=file(表示文件上传)的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。
为什么要用使用application/x-www-form-urlencoded作为默认方式,而不是multipart/form-data?
答:因为在使用form-data格式时,消息体中每一个部分都要加上像Content-Type、Content-Disposition这样MIME头,对于较短的字母数字数据(大部分web表单),使用application/x-www-form-urlencoded方式,编码的代码更小。
参考:https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
参考的stackoverflow答案如下:
Summary; 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.
The MIME types you mention are the two Content-Type headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.
For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equals symbol (=). An example of this would be:
MyVariableOne=ValueOne&MyVariableTwo=ValueTwo
According to the specification:
[Reserved and] non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character
That means that for each non-alphanumeric byte that exists in one of our values, it's going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.
That's where multipart/form-data comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type, and particularly Content-Disposition, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).
Why not use multipart/form-data all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.
multipart/form-data和application/x-www-form-urlencoded区别的更多相关文章
- (转载)http协议的Request Payload 和 Form Data 的区别
我正在开发的项目前端和后端是完全独立的,通过配置 webpack 的 proxy 将前端请求跨域代理到后台服务.昨天发现,我前端执行 post 请求,后台 springmvc 的 @RequestMa ...
- springMVC接收参数的区别form data与query string parameters与request payload
在AJAX请求中,我见过有三种form表单数据类型提交. 第一种:form data, 第二种:query string parameters,第三种:request payload. 在google ...
- form data和request payload的区别
HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp&qu ...
- HTTP请求中的form data和request payload的区别
HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp&qu ...
- [整理]Ajax Post请求下的Form Data和Request Payload
Ajax Post请求下的Form Data和Request Payload 通常情况下,我们通过Post提交表单,以键值对的形式存储在请求体中.此时的reqeuest headers会有Conten ...
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
转载:http://blog.csdn.net/mhmyqn/article/details/25561535 HTTP请求中,如果是get请求,那么表单参数以name=value&name1 ...
- AJAX POST请求中參数以form data和request payload形式在servlet中的获取方式
HTTP请求中,假设是get请求,那么表单參数以name=value&name1=value1的形式附到url的后面,假设是post请求,那么表单參数是在请求体中,也是以name=value& ...
- Vue中应用CORS实现AJAX跨域,及它在 form data 和 request payload 的小坑处理
基本概念部分(一):理解CORS 说道Vue的跨域AJAX,我想先梳理一遍CORS跨域,"跨域资源共享"(Cross-origin resource sharing),它是一个W3 ...
- 【转】HTTP请求中的form data和request payload的区别
jQuery的ajax方法和post方法分别发送请求,在后台Servlet进行处理时结果是不一样的,比如用$.ajax方法发送请求时(data参数是一个JSON.stringify()处理后的字符串, ...
- [转]AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
转载至 http://blog.csdn.net/mhmyqn/article/details/25561535 最近在写接收第三方的json数据, 因为对java不熟悉,有时候能通过request能 ...
随机推荐
- iOS11自定义导航条上移处理
image.png 在自定义导航条时,通常会继承系统的UINavigationBar,但如上图,在iOS11上,导航条改动了.自定义导航条代码 -(MBNavigationBar *)myNavBar ...
- jQuery-4.动画篇---淡入淡出效果
jQuery中淡出动画fadeOut 让元素在页面不可见,常用的办法就是通过设置样式的display:none.除此之外还可以一些类似的办法可以达到这个目的.这里要提一个透明度的方法,设置元素透明度为 ...
- Sql Server 默认值
--1.取得数据库所有表的默认值: select t3.name as 表名,t1.name as 字段名,t2.text as 默认值 ,t4.name from syscolumns t1,sys ...
- Lua实现Map
通过Lua中自带的table来实现一个Map,可以根据键值来插入移除取值 map = {} local this = map function this:new() o = {} setmetatab ...
- sqlalchemy执行sql语句
#删除user表中的所有数据,表中的自增字段又从默认值开始 session.execute("truncate table user")
- ScrimState.java
/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- 什么是BFC
对css有了解的朋友肯定都知道盒式模型这个概念,对一个元素设置css,首先需要知道这个元素是block还是inline类型.而BFC就是用来格式化块级盒子,同样管理inline类型的盒子还有IFC,以 ...
- 自定义EL函数(以将字母转为大写为例)
Step1 定义一个类:StringFunction.java 主要作用是来提供转大写的方法; public class StringFunction { public static String t ...
- ubuntu下绑定串口
查看有哪些设备连接在你的电脑上 lsusb 得到如图: 查看usb串口上连接的信息,得到不一样的信息 dmesg | grep ttyS* 我使用了一个usb扩展器,这边可以看到,被连接在ttyUSB ...
- 【转载】 spring事物配置,声明式事务管理和基于@Transactional注解的使用
https://blog.csdn.net/bao19901210/article/details/41724355