简体   繁体   English

REST Web 服务:错误代码应该是 404 还是 405?

[英]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:假设我们有一个具有以下路径的 Web 服务:

/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默认情况下,如果用户没有指定subscriptionId ,我们会得到405 Method not allowed但如果我们省略customerId ,我们会得到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? [编辑] 还有一件事:如果我们假设,例如,只有 DELETE 方法......你的答案也会一样吗? Thanks!谢谢!

  • 404 Not found is appropriate when the requested resource is not available请求的资源不可用时, 404 Not found是合适的
  • 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 ). 405 Method not allowed当资源不支持 HTTP 方法时(例如,对于只读资源,可能允许GET但不允许POSTPUTDELETE )。

When the user does not specify the subscriptionId , the requested resource would be /customer-web/customers/{customerId}/subscription/ .当用户未指定subscriptionId ,请求的资源将为/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 .该资源可能存在(并列出所有订阅),也可能不存在(未绑定到该资源),在这种情况下404 Not found将是合适的

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.现在,他们必须搜索该 ID/行号(假设行号为您的用户 ID)以检查您是否是适用的用户。

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.如果他们找到您,那么他们会将您的身份验证者 ID 或用户 ID 订阅到事件表的subscribedUser列,并将该事件 subscriptionId 添加到您行的subscribedEvents列。 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) CustomerID/UserID(这可以是该用户/客户的行索引号,它将被推送到该事件的subscribedUser列)
  • 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) EventID(这可以是该事件的行索引号,它将用作subscriptionId ,它将被推送到该subscribedEvent /客户行的subscribedEvent列)

Now, if one the ID above is missing then with what you will search for?现在,如果缺少上面的 ID,那么您将搜索什么? Probably you will have a null |可能你会有一个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. undefined如果您不提供其中任何一个并且行号或 UserID/EventId 不能为空,因为它们是数字并且也是唯一的。 So easy guess, the query operation would return with a empty [] or even null .很容易猜测,查询操作将返回空[]甚至null And null is a falsy value.而 null 是一个假值。 So the server will definitely treat this as an error & will send back 404 as Not Found response.因此,服务器肯定会将其视为错误并将404作为Not Found响应发回。

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.但是有一点,如果customerId用作参数:parameter URI 的:parameter ,如果未传递,则 URL 将指向另一个端点,并将返回其他/不相关的响应。 If that endpoint doesn't exists, then it would return another 404 error.如果该端点不存在,那么它将返回另一个 404 错误。 Same goes to the subscriptionId .同样适用于subscriptionId Above criterea only applies to wrong customerId or subscriptionId .以上标准仅适用于错误的customerIdsubscriptionId 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.现在,如果您不通过它们中的任何一个,那么地址将是错误的,如果该端点中不存在任何内容,则会发送404 ,如果端点存在,则会发送其他响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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