When you make a POST request, you have to encode the data that forms the body of the request in some way.
(发出POST请求时,必须以某种方式对构成请求主体的数据进行编码。)
HTML forms provide three methods of encoding.
(HTML表单提供了三种编码方法。)
Work was being done on adding application/json
, but that has been abandoned.
(正在添加application/json
,但已被放弃。)
(Other encodings are possible with HTTP requests generated using other means than an HTML form submission.)
((使用HTML表单提交以外的其他方式生成的HTTP请求也可以使用其他编码。))
The specifics of the formats don't matter to most developers.
(格式的细节对大多数开发人员而言并不重要。)
The important points are: (要点是:)
- Never use
text/plain
. (切勿使用text/plain
。)
When you are writing client-side code:
(在编写客户端代码时:)
When you are writing server-side code:
(在编写服务器端代码时:)
- Use a prewritten form handling library
(使用预先编写的表单处理库)
Most (such as Perl's CGI->param
or the one exposed by PHP's $_POST
superglobal) will take care of the differences for you.
(大多数(例如Perl的CGI->param
或PHP的$_POST
超全局变量公开的CGI->param
)将为您解决这些差异。)
Don't bother trying to parse the raw input received by the server. (不要费力尝试解析服务器收到的原始输入。)
Sometimes you will find a library which can't handle both formats.
(有时您会发现一个无法处理两种格式的库。)
Node.js's most popular library for handling form data is body-parser which cannot handle multipart requests (but has documentation which recommends some alternatives which can). (Node.js最受欢迎的用于处理表单数据的库是body-parser ,它无法处理多部分请求(但是有文档建议一些可能的替代方法)。)
If you are writing (or debugging) a library for parsing or generating the raw data, then you need to start worrying about the format.
(如果要编写(或调试)用于解析或生成原始数据的库,则需要开始担心格式。)
You might also want to know about it for interest's sake. (为了您的利益,您可能还想了解它。)
application/x-www-form-urlencoded
is more or less the same as a query string on the end of the URL.
(application/x-www-form-urlencoded
与URL末尾的查询字符串大致相同。)
multipart/form-data
is significantly more complicated but it allows entire files to be included in the data.
(multipart/form-data
要复杂得多,但是它允许整个文件包含在数据中。)
An example of the result can be found in the HTML 4 specification . (结果的一个示例可以在HTML 4规范中找到 。)
text/plain
is introduced by HTML 5 and is useful only for debugging — from the spec : They are not reliably interpretable by computer — and I'd argue that the others combined with tools (like the Net tab in the developer tools of most browsers) are better for that).
(text/plain
由HTML 5引入,仅在调试时有用-从规范上来说 : 它们不能由计算机可靠地解释 -我认为其他结合了工具(例如大多数浏览器的开发人员工具中的Net标签) )对此更好)。)