REST中的资源不支持操作时的HTTP状态代码404或501 [英] HTTP Status Code 404 or 501 when Operation is not supported by a Resource in REST

查看:136
本文介绍了REST中的资源不支持操作时的HTTP状态代码404或501的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST服务,并且根据正在查看的资源类型,我可以进行某些操作

I have a REST Service and depending on the type of Resource that is being viewed I have certain Operations avialable

所以Resource1支持Operation1Operation2

例如:

Resource1/Operation1

Resource1/Operation2

Resource2仅支持Operation1

例如:

Resource2/Operation1

因此,如果我在Resource2上收到对Operation2的呼叫(例如:Resource2/Operation2),则由于Operation2有效,服务器将不会引发错误,但是内部不支持该错误,因此我应该返回一个404-未找到,还是501-未实现会更准确,还是出现其他错误代码?

So if I receive a call for Operation2 on Resource2 (eg: Resource2/Operation2) the server will not raise an error as Operation2 is valid, but internally I don't support it, so should I return a 404 - Not Found or would a 501 - Not Implemented be more accurate or another error code?

推荐答案

您必须考虑资源(存在并对其进行操作)而不是服务(如WebServices)(您称其做某事).

You have to think in terms of Resources (that exist and you operate on them) and not Services as in WebServices (which you call to do something).

因此,REST API的每个 Operation 本身都是一种资源(即,对长时间运行的操作或通过发送 POST 触发的某种更复杂的事务进行建模)对操作资源的请求)-并且该资源的URI是任何不透明的字符串,可以是'Resource1/Operation1'或"/someOther/arbitrary/path",

So every Operation of your REST API is a resource by itself (i.e. to model long running operations or some sort of more complex transaction that are triggered by sending a POST request to the operation resource) - and the URI to that resource is any opaque string, which can be 'Resource1/Operation1' or "/someOther/arbitrary/path",

A 501表示HTTP服务器本身具有未实现的,必须满足请求的特定功能-即它缺乏某些技术功能-但资源本身是有效的.

A 501 means the HTTP server itself has a particular feature not implemented that is require to fulfill the request - i.e. it lacks some technical features - but the resource itself is valid.

但是,正如您所写的那样,您的Rest API根本不支持Resource2的Operation2,这意味着不存在诸如"Resource2/Operation2"服务资源之类的东西.因此404响应是唯一合理的选择.

But as you've written, your Rest API is not designed to support Operation2 for Resource2 at all, which means that there is no such thing as a "Resource2/Operation2" service resource. Therefore 404 response is the only reasonable choice.

在客户端将如何交互..."方面,问自己:鉴于是资源(即某个任意网站)的使用者,在接收时您是否会重新访问相同的URL 404? (可能不会)(如果没有),您会收到501吗? (可能是的,因为您认为问题已经解决了)

In terms of "how client will interact...", ask yourself: given you are the consumer of a resource (i.e. some arbitrary web site), will you revisit the same URL when receiving a 404? (probably not) will you do when receiving a 501? (probably yes, as you assume the problem has been resolved in the meantime)

TL; DR

返回404

这篇关于REST中的资源不支持操作时的HTTP状态代码404或501的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆