POST vs PUT vs DELETE [英] POST vs PUT vs DELETE
问题描述
我从来没有真正明白为什么我需要PUT和删除请求方法。
I never really understood why do I need the PUT and Delete request methods.
在我的代码中我一直在使用帖子
然后执行后端代码添加/删除内容。
In my code I'm using all the time post
and just do the backend code to add/delete something.
我这样做错了还是可以一直使用POST?
Am I doing this wrong or is it ok to use POST all the time?
我的例子:
@RequestMapping(value = "/delete-images", method = RequestMethod.POST)
public @ResponseBody void deleteImages(@RequestParam("imageIDs") String[] imageIDs) {
Docker.deleteImages(imageIDs);
}
推荐答案
这都是关于请求的语义。来自 RFC 7231 :
请求方法标记是请求语义的主要来源;
它表示客户提出此请求的目的
以及客户期望的成功结果。
The request method token is the primary source of request semantics; it indicates the purpose for which the client has made this request and what is expected by the client as a successful result.
以下是RFC 7231中定义的一些HTTP方法的简要说明(单击链接以检查完整的方法定义):
Here's a brief description of some HTTP methods defined in the RFC 7231 (click the links to check the full method definition):
-
GET
:传输目标资源的当前表示。 -
HEAD
:与GET
相同,但仅传输状态行和标题部分。 -
POST
:对请求有效负载执行特定于资源的处理。 -
PUT
:用请求有效负载替换目标资源的所有当前表示。 -
DELETE
:删除目标资源的所有当前表示
GET
: Transfer a current representation of the target resource.HEAD
: Same asGET
, but only transfer the status line and header section.POST
: Perform resource-specific processing on the request payload.PUT
: Replace all current representations of the target resource with the request payload.DELETE
: Remove all current representations of the target resource
这篇关于POST vs PUT vs DELETE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!