HTTP POST
方法 发送数据给服务器. 请求主体的类型由 Content-Type
首部指定.
PUT 和POST
方法的区别是,PUT方法是幂等的:连续调用一次或者多次的效果相同(无副作用)。连续调用同一个POST可能会带来额外的影响,比如多次提交订单。
一个 POST
请求通常是通过 HTML 表单发送, 并返回服务器的修改结果. 在这种情况下, content type 是通过在 <form>
元素中设置正确的 enctype
属性, 或是在 <input>
和 <button>
元素中设置 formenctype
属性来选择的:
application/
x-www-form-urlencoded
: 数据被编码成以'&'
分隔的键-值对, 同时以'='
分隔键和值. 非字母或数字的字符会被 percent-encoding: 这也就是为什么这种类型不支持二进制数据的原因 (应使用multipart/form-data
代替).multipart/form-data
text/plain
当 POST 请求是通过除 HTML 表单之外的方式发送时, 例如使用 XMLHttpRequest
, 那么请求主体可以是任何类型.按HTTP 1.1规范中描述,POST为了以统一的方法来涵盖以下功能:
- 注释已有的资源
- 在公告板,新闻组,邮件列表或类似的文章组中发布消息;
- 通过注册新增用户;
- 向数据处理程序提供一批数据,例如提交一个表单;
- 通过追加操作,扩展数据库数据.
请求是否有主体 | 是 |
---|---|
成功的响应是否有主体 | 是 |
Safe | 否 |
Idempotent | 否 |
Cacheable | Only if freshness information is included |
Allowed in HTML forms | 是 |
语法
POST /index.html
示例
使用默认的 application/x-www-form-urlencoded
做为 content type 的简单表单:
POST / HTTP/1.1 Host: foo.com Content-Type: application/x-www-form-urlencoded Content-Length: 13 say=Hi&to=Mom
使用 multipart/form-data
作为 content type 的表单:
POST /test.html HTTP/1.1 Host: example.org Content-Type: multipart/form-data;boundary="boundary" --boundary Content-Disposition: form-data; name="field1" value1 --boundary Content-Disposition: form-data; name="field2"; filename="example.txt" value2
规范
规范 | 标题 |
---|---|
RFC 7231, section 4.3.3: POST | Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content |
浏览器兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support Yes | Edge Full support Yes | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support