本文介绍了HTTP方法:DELETE与POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为Session的资源。客户端将调用PUT来创建并开始新会话。当客户端完成会话后,它将不再可访问,但应该因历史/问责原因而持续存在。

Let's say I have a resource called "Session". The client would call PUT to create and begin a new session. When the client is finished with the session, it should no longer be accessible, but should persist for historical/accountability reasons.

要结束会话,是否更合适发出一个DELETE请求,看起来在语义上更接近于所需的效果,或POST,因为资源实际上并没有被永久删除?

To end the session, would it be more appropriate to issue a DELETE request, which would seem semantically closer to the desired effect, or POST, seeing as the resource isn't actually removed permanently?

推荐答案

这里的问题是:请求?如果您执行两次相同的请求,是否有副作用?就像你订购一篇文章一样,两次执行订单请求会得到两次文章。

The question here is: Is the request idempotent? If you execute the same request twice, does it have a side effect? Like when you order an article, executing the order request twice would get you the article twice.

在这种情况下, POST 是您想要的方法。如果没有,那么你想要 PUT DELETE

In that case, POST is the method you want. If not, then you want either PUT or DELETE.

因为你似乎没有删除会话,只改变它的状态, PUT 将是一个更好的方法,因为它意味着资源被改变了,而不是删除,在您的情况下就是这种情况。

As you don't seem to be deleting the session, only altering its state, PUT would be a better method, because it means that the resource is altered, and not deleted, which is the case in your case.

编辑:

如果资源似乎从客户端删除, DELETE 似乎更合适。如何在后面实现事情对客户来说无关紧要。

If the resource appears to be deleted from the client, DELETE seems more appropriate. How things are implemented in the back doesn't matter for the client.

这篇关于HTTP方法:DELETE与POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:00