HTTP几种请求方式的含义及应用场景

HTTP几种请求方式的含义及应用场景

整理自https://stackoverflow.com/questions/27030649/explain-and-example-about-get-delete-post-put-options-patch-hhttps://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

GET

GET请求是向服务端请求获取某个或某些资源(resource),比如查询数据库单个或list数据,服务端成功的话,一般状态码返回200。

POST

POST请求是用来向服务端请求新增资源(resource),处理成功的话,服务端一般返回状态码201。

PUT

PUT请求一般是用来向服务端请求修改某个已存在的资源(resource),服务端一般返回状态码200,204等。

DELETE

DELETE请求一般是用来向服务端请求删除某个已存在的资源(resource),服务端一般返回200,202等。

PATCH

PATCH请求一般是对某个资源做局部修改,如个别字段。

PUT和PATCH区别:PUT和PATCH都是用来修改服务端某个资源的,但是PUT和PATCH修改时提交的数据是不同的,PUT是将整个资源的信息都提交到服务端,包括修改的,未修改的都提交到服务端,而PATCH只提交已修改的字段到服务端。而服务端对PUT请求应该是整体替换,PATCH请求只修改提交的字段。所以PUT请求应该是幂等的,即多次提交同一个请求,结果是相同的。

来自Stack Overflow:

https://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples

 

TRACE

TRACE请求没有使用过。w3网站上是这么介绍的:

`The TRACE method is used to invoke a remote, application-layer loop- back of the request message. The final recipient of the request SHOULD reflect the message received back to the client as the entity-body of a 200 (OK) response. The final recipient is either the

origin server or the first proxy or gateway to receive a Max-Forwards value of zero (0) in the request (see section 14.31). A TRACE request MUST NOT include an entity.

TRACE allows the client to see what is being received at the other end of the request chain and use that data for testing or diagnostic information. The value of the Via header field (section 14.45) is of particular interest, since it acts as a trace of the request chain. Use of the Max-Forwards header field allows the client to limit the length of the request chain, which is useful for testing a chain of proxies forwarding messages in an infinite loop.

If the request is valid, the response SHOULD contain the entire request message in the entity-body, with a Content-Type of "message/http". Responses to this method MUST NOT be cached.`

OPTIONS

OPTIONS请求一般是客户端向服务端判断对某个资源是否有访问权限。

HEAD

HEAD请求一般是用来获取某个资源的metadata信息,比如说某份报告的关键描述信息等。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值