gpt4 book ai didi

RESTful API : Delete Entity - What should I return as result?

转载 作者:行者123 更新时间:2023-12-04 01:55:47 25 4
gpt4 key购买 nike

我正在实现一个 RESTful API,一个 Controller 的端点是 Delete。 Delete 根据要删除的实体执行两个操作:更新实体或从数据库中删除实体。如果删除更新实体,我将发送 HttpStatus 200和更新的实体。但如果删除从数据库中删除实体,我将只发送 HttpStatus 200 .在一种情况下,我正在返回一个对象。但在另一种情况下,该对象不再存在。这是一个好方法还是我错过了什么?

最佳答案

简短的回答
DELETE 的合适状态码成功的请求是 200 , 202 204 .
长答案

DELETE does two actions depending of the entity to be removed: it updates the entity or it deletes the entity from the database. [...] Is it a good approach or I am missing anything?


DELETE 方法不适用于执行更新。
请参阅 RFC 7231 中的以下引用,定义 HTTP/1.1 协议(protocol)的文档之一,详细了解 DELETE 方法是关于:

4.3.5. DELETE

The DELETE method requests that the origin server remove theassociation between the target resource and its currentfunctionality. In effect, this method is similar to the rm commandin UNIX: it expresses a deletion operation on the URI mapping of theorigin server rather than an expectation that the previouslyassociated information be deleted. [...]


如果删除操作成功,服务器可以返回以下状态码之一:
  • 202 :表示已接受请求进行处理,但处理尚未完成。
  • 204 : 表示服务器有
    成功完成了请求并且没有额外的
    要在响应有效负载正文中发送的内容。
  • 200 :表示请求已成功,并且请求有效负载包含操作状态的表示。

  • 请参阅同一文档中的以下引用:

    If a DELETE method is successfully applied, the origin server SHOULDsend a 202 (Accepted) status code if the action will likely succeedbut has not yet been enacted, a 204 (No Content) status code if theaction has been enacted and no further information is to be supplied,or a 200 (OK) status code if the action has been enacted and theresponse message includes a representation describing the status.

    关于RESTful API : Delete Entity - What should I return as result?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792505/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com