1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
---
title: POST
slug: Web/HTTP/Methods/POST
translation_of: Web/HTTP/Methods/POST
---
<div>{{HTTPSidebar}}</div>
<p>The <strong>HTTP <code>POST</code> method</strong> sends data to the server. The type of the body of the request is indicated by the {{HTTPHeader("Content-Type")}} header.</p>
<p>The difference between <code>PUT</code> and {{HTTPMethod("POST")}} is that <code>PUT</code> is idempotent: calling it once or several times successively has the same effect (that is no <em>side</em> effect), where successive identical <code>POST</code> may have additional effects, like passing an order several times.</p>
<p>A <code>POST</code> request is typically sent via an <a href="/en-US/docs/Web/Guide/HTML/Forms">HTML form</a> and results in a change on the server. In this case, the content type is selected by putting the adequate string in the {{htmlattrxref("enctype", "form")}} attribute of the {{HTMLElement("form")}} element or the {{htmlattrxref("formenctype", "input")}} attribute of the {{HTMLElement("input") }} or {{HTMLElement("button")}} elements:</p>
<ul>
<li><code>application/x-www-form-urlencoded</code>: the keys and values are encoded in key-value tuples separated by <code>'&'</code>, with a <code>'='</code> between the key and the value. Non-alphanumeric characters in both keys and values are {{glossary("percent-encoding", "percent encoded")}}: this is the reason why this type is not suitable to use with binary data (use <code>multipart/form-data</code> instead)</li>
<li><code>multipart/form-data</code>: each value is sent as a block of data ("body part"), with a user agent-defined delimiter ("boundary") separating each part. The keys are given in the <code>Content-Disposition</code> header of each part.</li>
<li><code>text/plain</code></li>
</ul>
<p>When the <code>POST</code> request is sent via a method other than an HTML form — like via an {{domxref("XMLHttpRequest")}} — the body can take any type. As described in the HTTP 1.1 specification, <code>POST</code> is designed to allow a uniform method to cover the following functions:</p>
<ul>
<li>Annotation of existing resources</li>
<li>Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;</li>
<li>Adding a new user through a signup modal;</li>
<li>Providing a block of data, such as the result of submitting a form, to a data-handling process;</li>
<li>Extending a database through an append operation.</li>
</ul>
<table class="properties">
<tbody>
<tr>
<th scope="row">Request has body</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">Successful response has body</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">{{Glossary("Safe")}}</th>
<td>No</td>
</tr>
<tr>
<th scope="row">{{Glossary("Idempotent")}}</th>
<td>No</td>
</tr>
<tr>
<th scope="row">{{Glossary("Cacheable")}}</th>
<td>Only if freshness information is included</td>
</tr>
<tr>
<th scope="row">Allowed in <a href="/en-US/docs/Web/Guide/HTML/Forms">HTML forms</a></th>
<td>Yes</td>
</tr>
</tbody>
</table>
<h2 id="格式">格式</h2>
<pre class="syntaxbox">POST /test
</pre>
<h2 id="範例">範例</h2>
<p>使用 <code>application/x-www-form-urlencoded</code> 內容類型的簡易表單:</p>
<pre class="line-numbers language-html">POST /test HTTP/1.1
Host: foo.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
field1=value1&field2=value2</pre>
<p>使用 <code>multipart/form-data</code> 內容類型的表單:</p>
<pre>POST /test HTTP/1.1
Host: foo.example
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
--boundary--
</pre>
<h2 id="規範">規範</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">規範</th>
<th scope="col">標題</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{RFC("7231", "POST", "4.3.3")}}</td>
<td>Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content</td>
</tr>
<tr>
<td>{{RFC("2046", "Common Syntax", "5.1.1")}}</td>
<td>Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types</td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
<p>{{Compat("http.methods.POST")}}</p>
<h2 id="參見">參見</h2>
<ul>
<li>{{HTTPHeader("Content-Type")}}</li>
<li>{{HTTPHeader("Content-Disposition")}}</li>
</ul>
|