如何指示 REST API 端点已删除

How do I indicate a REST API endpoint is removed

我有一个端点

DELETE /events/<id>

在API的新版本中,我们将删除这个API

现在如果客户端请求相同URL我应该输出什么状态码?

我找到了几个选项。

  1. 410 Gone。这是最近的一个。但是跟资源有关。 这与管理 API 无关。我认为这意味着,如果 GET /events/1 有效,那么声明 /events/1Gone
  2. 是没有意义的
  3. 301 Moved Permanently302 Found。它们用于重定向。但替换不是 GET 调用
  4. 405 Method Not Allowed。这是有道理的,因为现在不再允许 DELETE 方法。但是它没有说过去允许它(不确定我们是否需要它)

此端点支持的其他 http 方法是,

GET /events/<id>/
POST /events/
POST /events/<id>/actions/

澄清一下。当我 google 该主题时,我会获得有关资源的更多信息。但在这里我关心的是 API 的折旧和移除。更像是一种管理。

你这里有些东西扭曲了。

DELETE 是动词,表示方法,而不是端点。

/events这是你的端点。

MDN 建议 410 Gone 已删除的资源。通用 http 上下文中的资源可以是任何内容。

The HyperText Transfer Protocol (HTTP) 410 Gone client error response code indicates that access to the target resource is no longer available at the origin server and that this condition is likely to be permanent.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/410

如果你想显示一个方法已被删除但端点仍然存在,你可能会使用 405 Method not allowed.

您还应该考虑对 API 进行版本控制。