HTTP PUT 请求方法使用请求中的负载创建或者替换目标资源。
PUT
与 POST
方法的区别在于,PUT方法是幂等的:调用一次与连续调用多次是等价的(即没有副作用),而连续调用多次POST方法可能会有副作用,比如将一个订单重复提交多次。
Request has body | Yes |
---|---|
Successful response has body | No |
Safe | No |
Idempotent | Yes |
Cacheable | No |
Allowed in HTML forms | No |
语法
PUT /new.html HTTP/1.1
示例
请求
PUT /new.html HTTP/1.1 Host: example.com Content-type: text/html Content-length: 16 <p>New File</p>
应答
如果目标资源不存在,并且PUT方法成功创建了一份,那么源头服务器必须返回201
(Created
) 来通知客户端资源已创建。
HTTP/1.1 201 Created Content-Location: /new.html
如果目标资源已经存在,并且依照请求中封装的表现形式成功进行了更新,那么,源头服务器必须返回200
(OK
) 或者204
(No Content
) 来表示请求的成功完成。
HTTP/1.1 204 No Content Content-Location: /existing.html
规范
Specification | Title |
---|---|
RFC 7231, section 4.3.4: PUT | Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content |
浏览器兼容性
BCD tables only load in the browser
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.