json HTTP POST 的表单内容类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form content type for a json HTTP POST?
提问by rmk
Just wanted a clarification of the form content types:
只是想澄清表单内容类型:
application/x-www-form-urlencoded: This is where you can send params encoded with the url.multipart/form-data: ??
application/x-www-form-urlencoded:这是您可以发送使用 url 编码的参数的地方。multipart/form-data: ??
I need to send a JSON in the post (so it would have the type: text/x-json, I guess).
So the question is, is multipart/form-datasuitable for this purpose / is application/x-www-form-urlencodedbetter?
我需要在帖子中发送一个 JSON(所以它的类型是:text/x-json,我猜)。所以问题是,multipart/form-data适合这个目的/application/x-www-form-urlencoded更好吗?
Also, would it be possible to send some params encoded in the url, and some data in the json?
另外,是否可以发送一些编码在 url 中的参数和一些 json 中的数据?
回答by Herman J. Radtke III
It looks like people answered the first part of your question (use application/json).
看起来人们回答了您问题的第一部分(使用 application/json)。
For the second part: It is perfectly legal to send query parameters in a HTTP POST Request.
对于第二部分:在 HTTP POST 请求中发送查询参数是完全合法的。
Example:
例子:
POST /members?id=1234 HTTP/1.1
Host: www.example.com
Content-Type: application/json
{"email":"[email protected]"}
Query parameters are commonly used in a POST request to refer to an existing resource. The above example would update the email address of an existing member with the id of 1234.
查询参数通常用于 POST 请求以引用现有资源。上面的示例将更新 ID 为 1234 的现有成员的电子邮件地址。
回答by user132447
I have wondered the same thing. Basically it appears that the html spec has different content types for html and form data. Json only has a single content type.
我也想过同样的事情。基本上,html 规范似乎对 html 和表单数据具有不同的内容类型。Json 只有一种内容类型。
According to the spec, a POST of json data should have the content-type:
application/json
根据规范,json 数据的 POST 应该具有内容类型:
application/json
Relevant portion of the HTML spec
HTML 规范的相关部分
6.7 Content types (MIME types)
...
Examples of content types include "text/html", "image/png", "image/gif", "video/mpeg", "text/css", and "audio/basic".17.13.4 Form content types
...
application/x-www-form-urlencoded
This is the default content type. Forms submitted with this content type must be encoded as follows
6.7 内容类型(MIME 类型)
……
内容类型的示例包括“text/html”、“image/png”、“image/gif”、“video/mpeg”、“text/css”和“audio/basic” ”。17.13.4 表单内容类型
...
application/x-www-form-urlencoded
这是默认的内容类型。使用此内容类型提交的表单必须编码如下
Relevant portion of the JSON spec
JSON 规范的相关部分
- IANA Considerations
The MIME media type for JSON text is application/json.
- IANA 注意事项
JSON 文本的 MIME 媒体类型是 application/json。
回答by Lorenzo
multipart/form-data
is used when you want to upload files to the server. Please check this articlefor details.
当您要将文件上传到服务器时使用。详情请查看这篇文章。

