HTTP request 方法

GET

描述包含
request 中有 bodyNo
response 成功时有 bodyYes
安全(safe)Yes
幂等(idempotent)Yes
可缓存(cacheable)Yes
允许 HTML 表单Yes

POST

描述包含
request 中有 bodyYes
response 成功时有 bodyYes
安全(safe)No
幂等(idempotent)No
可缓存(cacheable)仅当包含保鲜信息时
允许 HTML 表单Yes

PUT

描述包含
request 中有 bodyYes
response 成功时有 bodyNo
安全(safe)No
幂等(idempotent)Yes
可缓存(cacheable)No
允许 HTML 表单No

DELETE

描述包含
request 中有 bodyMay
response 成功时有 bodyMay
安全(safe)No
幂等(idempotent)Yes
可缓存(cacheable)No
允许 HTML 表单

No

PUT 和 POST

何时使用 HTTP PUT 和 HTTP 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值