--- title: POST slug: Web/HTTP/Methods/POST tags: - HTTP - 参考 - 请求方法 translation_of: Web/HTTP/Methods/POST ---
{{HTTPSidebar}}

HTTP POST 方法 发送数据给服务器. 请求主体的类型由 {{HTTPHeader("Content-Type")}} 首部指定.

PUT 和{{HTTPMethod("POST")}}方法的区别是,PUT方法是幂等的:连续调用一次或者多次的效果相同(无副作用)。连续调用同一个POST可能会带来额外的影响,比如多次提交订单。

一个 POST 请求通常是通过 HTML 表单发送, 并返回服务器的修改结果. 在这种情况下, content type 是通过在 {{HTMLElement("form")}} 元素中设置正确的 {{htmlattrxref("enctype", "form")}} 属性, 或是在 {{HTMLElement("input") }} 和 {{HTMLElement("button")}} 元素中设置 {{htmlattrxref("formenctype", "input")}} 属性来选择的:

当 POST 请求是通过除 HTML 表单之外的方式发送时, 例如使用 {{domxref("XMLHttpRequest")}}, 那么请求主体可以是任何类型.按HTTP 1.1规范中描述,POST为了以统一的方法来涵盖以下功能:

请求是否有主体
成功的响应是否有主体
{{Glossary("安全")}}
{{Glossary("幂等")}}
{{Glossary("可缓存")}} Only if freshness information is included
HTML 表单是否支持

语法

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", "POST", "4.3.3")}} Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content

浏览器兼容性

{{Compat("http.methods.POST")}}

另见