aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/http/methods/put/index.html
blob: eeddfdd5c8e48c16f37ac9efb540b3772601b0aa (plain)
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
---
title: PUT
slug: Web/HTTP/Methods/PUT
tags:
  - HTTP
  - HTTP method
  - Request method
  - put
translation_of: Web/HTTP/Methods/PUT
---
<div>{{HTTPSidebar}}</div>

<p><strong>HTTP PUT 请求方法</strong>使用请求中的负载创建或者替换目标资源。</p>

<p><code>PUT</code> 与 {{HTTPMethod("POST")}} 方法的区别在于,PUT方法是幂等的:调用一次与连续调用多次是等价的(即没有副作用),而连续调用多次POST方法可能会有副作用,比如将一个订单重复提交多次。</p>

<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>No</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Safe")}}</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Idempotent")}}</th>
   <td>Yes</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Cacheable")}}</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">Allowed in <a href="/en-US/docs/Web/Guide/HTML/Forms">HTML forms</a></th>
   <td>No</td>
  </tr>
 </tbody>
</table>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">PUT /new.html HTTP/1.1
</pre>

<h2 id="示例">示例</h2>

<h3 id="请求">请求</h3>

<pre>PUT /new.html HTTP/1.1
Host: example.com
Content-type: text/html
Content-length: 16

&lt;p&gt;New File&lt;/p&gt;</pre>

<h3 id="应答">应答</h3>

<p>如果目标资源不存在,并且PUT方法成功创建了一份,那么源头服务器必须返回{{HTTPStatus("201")}} (<code>Created</code>) 来通知客户端资源已创建。</p>

<pre class="newpage">HTTP/1.1 201 Created
Content-Location: /new.html</pre>

<p>如果目标资源已经存在,并且依照请求中封装的表现形式成功进行了更新,那么,源头服务器必须返回{{HTTPStatus("200")}} (<code>OK</code>) 或者{{HTTPStatus("204")}} (<code>No Content</code>) 来表示请求的成功完成。</p>

<pre>HTTP/1.1 204 No Content
Content-Location: /existing.html
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Title</th>
  </tr>
  <tr>
   <td>{{RFC("7231", "PUT", "4.3.4")}}</td>
   <td>Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content</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.PUT")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{HTTPStatus("201")}}</li>
 <li>{{HTTPStatus("204")}}</li>
</ul>