GET
描述 | 包含 |
request 中有 body | No |
response 成功时有 body | Yes |
安全(safe) | Yes |
幂等(idempotent) | Yes |
可缓存(cacheable) | Yes |
允许 HTML 表单 | Yes |
POST
描述 | 包含 |
request 中有 body | Yes |
response 成功时有 body | Yes |
安全(safe) | No |
幂等(idempotent) | No |
可缓存(cacheable) | 仅当包含保鲜信息时 |
允许 HTML 表单 | Yes |
PUT
描述 | 包含 |
request 中有 body | Yes |
response 成功时有 body | No |
安全(safe) | No |
幂等(idempotent) | Yes |
可缓存(cacheable) | No |
允许 HTML 表单 | No |
DELETE
描述 | 包含 |
request 中有 body | May |
response 成功时有 body | May |
安全(safe) | No |
幂等(idempotent) | Yes |
可缓存(cacheable) | No |
允许 HTML 表单 | No |
PUT 和 POST
PUT
The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource … the origin server can create the resource with that URI.
PUT 规范要求,你已经知道要创建或更新的资源的 URL。在创建时,如果客户端选择资源的标识符,PUT 请求将在指定的 URL 上创建新资源。
PUT /user/1234567890 HTTP/1.1
Host: http://sookocheff.com
{ "name": "Kevin Sookocheff", "website": "http://kevinsookocheff.com"}
server 可以使用 201 Created 状态码和新资源的位置进行响应。
HTTP/1.1 201 Created
Location: /user/1234567890
此外,如果你知道某个 URL 的资源已经存在,可以向该 URL 发出 PUT 请求,以替换该资源在服务器上的状态。此示例更新用户的网站。
PUT /user/1234567890 HTTP/1.1
Host: http://sookocheff.com
{ "name": "Kevin Sookocheff", "website": "http://kevinsookocheff.com"}
POST
实际而言,POST 用于将资源附加到现有集合。在下面的例子中,client 不知道资源的实际 URL,server 决定它在用户集合下的存储位置。
POST /user HTTP/1.1
Host: http://sookocheff.com
{ "name": "Bryan Larson", "website": "http://www.bryanlarson.ca"}
server 可以使用 201 Created 状态码和资源的新位置进行响应。
HTTP/1.1 201 Created
Location: /user/636363