Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Status Code 404 or 501 when Operation is not supported by a Resource in REST

Tags:

rest

http

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

So Resource1 supports Operation1 and Operation2

eg:

Resource1/Operation1

Resource1/Operation2

And Resource2 only supports Operation1

eg:

Resource2/Operation1

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?

like image 630
sbarnby71 Avatar asked Feb 10 '16 09:02

sbarnby71


1 Answers

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).

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 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.

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.

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

return 404

like image 121
Gerald Mücke Avatar answered Oct 12 '22 21:10

Gerald Mücke