REST api:无内容情况下的最佳实践

REST api: best practices in case of no content

我对 rest api 非常熟悉,但今天我被问到这个问题: "what if a resource exists but it's content have been blocked for some reason?"

示例: 尝试检索 users/18 但 ID 为 18 的用户已 banned/blocked/whatever。

我认为最好的答案是 return 204,但是如何同时通知客户资源不可用的原因?

是否有最佳实践?

I think the best answer would be to return 204, but how to also inform the client of the reason for the resource is not available to you?

不,204 不是一个好的选择 -- 特别是因为 204 不能 contain a message body

我希望

403 Forbidden

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it. A server that wishes to make public why the request has been forbidden can describe that reason in the response payload (if any).

404 Not Found

An origin server that wishes to "hide" the current existence of a forbidden target resource MAY instead respond with a status code of 404 (Not Found).

451 Unavailable For Legal Reasons

This status code indicates that the server is denying access to the resource as a consequence of a legal demand.