HTTP Status Code 404 or 501 when Operation is not

2020-07-07 02:29发布

问题:

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?

回答1:

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



回答2:

Never return any HTTP 5XX status from you code. It's used by the server to report server internal problem. If you are using that it would be confusing for the application user. So just use 404 for your purpose.



回答3:

You can use either of them,

its your webapis , depend upon how the client will interact with it.

4xx is for client error and 5xx is for server error

you may use 501 Not Implemented instead of 404 not found

because the resource in url is not there .

OR you can also use

your own custom error code and error message.

or

422 Unprocessable Entity The request was well-formed but was unable to be followed due to semantic errors.



回答4:

If the client request by definition is illegal, I would return HTTP 400 Bad Request.

Sending HTTP 400 also implies that the request should not merely be retried, as it is permanently illegal and is not understood by he server.

Only reservation I would have with using HTTP 400 is that it usually seems to be used for malformed requests; i.e. typos and the like. Whether your request is illegal or malformed, is a question of semantics.



标签: rest http