简体   繁体   中英

REST web service: Error code should be 404 or 405?

I would like to ask you what error code should I return in this case:

Let's assume we have a web service with this path:

/customer-web/customers/{customerId}/subscription/{subscriptionId}

By default, if the user does not specify the subscriptionId , we get a 405 Method not allowed but, if we omit the customerId instead, we get a 404 Not found

Is that correct? should we unify the error codes in some way?.

Thank you!

[EDIT] Just one more thing: If we assume, for example, DELETE method only... Will your answers be also the same? Thanks!

  • 404 Not found is appropriate when the requested resource is not available
  • 405 Method not allowed is to be used when the HTTP method is not supported on the resource (eg for read-only resources, GET may be allowed but not POST , PUT or DELETE ).

When the user does not specify the subscriptionId , the requested resource would be /customer-web/customers/{customerId}/subscription/ .
That resource may exist (and list all subscriptions), or may not exist (no binding to that resource), in which case the 404 Not found would be appropriate .

Yeah, it's logically incorrect.

Let's say you are a customer and you have some data/information about yourself. These are saved in a database or other data store. Now, you are gonna be treated as a user of that service. Now, that service provides some features, for example: They can provide you the latest news about your favourite topic. Now you have to subscribe to that particular feature so that you can get notified. But to get yourself notified the server will first have to subscribe you to that event/feature. Every database/data store's Table's Row has a unique identifier eg Row number. Now, they have to search for that Id/Row Number (Presume the Row Number as your User Id) to check if you are an applicable user or not.

If they found you then they would subscribe your Identity Verifier ID or UserID to the Event Table's subscribedUser Column and also will add that event subscriptionId to your Row's subscribedEvents Column. Then if you are subscribed then they would send you notifications of your favourite topic or whatever.

In the above story, the things that are neccessery to create a subscription for a user/customer is:

  • CustomerID/UserID (This can be the Row's Index Number of that User/Customer which will be pushed to the subscribedUser column of that Event)
  • EventID (This can be the Row's Index Number of that Event which will used as the subscriptionId , which will be pushed to the subscribedEvent Column of thatUser/Customer's Row)

Now, if one the ID above is missing then with what you will search for? Probably you will have a null | undefined if you don't provide any of them & Row Number or UserID/EventId can't Null as they are Numbers and also unique. So easy guess, the query operation would return with a empty [] or even null . And null is a falsy value. So the server will definitely treat this as an error & will send back 404 as Not Found response.

But one thing, if the customerId is used as a parameter :parameter of the URI & if it isn't passed, then the URL will point to another endpoint & will return other/unrelated response. If that endpoint doesn't exists, then it would return another 404 error. Same goes to the subscriptionId . Above criterea only applies to wrong customerId or subscriptionId . Now, if you don't pass either of them then the address will be wrong & will send 404 if nothing exists in that endpoint & other response if endpoint exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM