application / x-www-form-urlencoded或multipart / form-data? [英] application/x-www-form-urlencoded or multipart/form-data?

查看:154
本文介绍了application / x-www-form-urlencoded或multipart / form-data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTTP中有两种POST数据的方式: application / x-www-form-urlencoded multipart / form-data 。据我所知,如果使用 multipart / form-data ,大多数浏览器只能上传文件。在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)?这可能是例如基于:

In HTTP there are two ways to POST data: application/x-www-form-urlencoded and multipart/form-data. I understand that most browsers are only able to upload files if multipart/form-data is used. Is there any additional guidance when to use one of the encoding types in an API context (no browser involved)? This might e.g. be based on:


  • 数据大小

  • 存在非ASCII字符

  • 存在于(未编码的)二进制数据

  • 需要传输其他数据(如文件名)

  • data size
  • existence of non-ASCII characters
  • existence on (unencoded) binary data
  • the need to transfer additional data (like filename)

到目前为止,我基本上没有在网上找到关于使用不同内容类型的正式指导。

I basically found no formal guidance on the web regarding the use of the different content-types so far.

推荐答案

TL; DR

摘要;如果您要传输二进制(非字母数字)数据(或显着大小的有效负载),请使用 multipart / form-data 。否则,请使用 application / x-www-form-urlencoded

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.

您提到的MIME类型是用户代理(浏览器)必须支持的HTTP POST请求的两个 Content-Type 标头。这两种类型的请求的目的是将名称/值对列表发送到服务器。根据传输的数据类型和数量,其中一种方法比另一种方法更有效。要了解原因,你必须看看每个人在做什么。

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.

对于 application / x-www-form-urlencoded ,发送到服务器的HTTP消息的主体实际上是一个巨大的查询字符串 - 名称/值对由&符号分隔(& ) ,名称通过等号( = )与值分隔。这方面的一个例子是: 

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

根据规范


[保留和]非字母数字字符由'%HH'替换,百分号和两个十六进制数字代表ASCII代码character

[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.

这就是 multipart / form-data 通过这种传输名称/值对的方法,每对在MIME消息中表示为部分(如其他答案所述)。部件由特定的字符串边界分隔(具体选择使得此边界字符串不会出现在任何值有效负载中)。每个部分都有自己的MIME标头集,如 Content-Type ,特别是 Content-Disposition ,它们可以为每个部分是名字。每个名称/值对的值部分是MIME消息的每个部分的有效负载。 MIME规范在表示值有效负载时为我们提供了更多选项 - 我们可以选择更有效的二进制数据编码来节省带宽(例如base 64甚至原始二进制)。

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).

为什么不一直使用 multipart / form-data ?对于简短的字母数字值(与大多数Web表单一样),添加所有MIME标头的开销将大大超过更有效的二进制编码所带来的节省。

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.

这篇关于application / x-www-form-urlencoded或multipart / form-data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆