英文:
REST web service: Error code should be 404 or 405?
问题
以下是您要求的翻译内容:
我想问您在这种情况下应该返回什么错误代码:
假设我们有一个具有以下路径的网络服务:
/customer-web/customers/{customerId}/subscription/{subscriptionId}
默认情况下,如果用户未指定subscriptionId
,我们会得到405 Method not allowed
,但是如果我们省略了customerId
,我们会得到404 Not found
。
这正确吗?我们应该以某种方式统一错误代码吗?
谢谢!
[编辑]
还有一件事:如果我们假设,仅使用DELETE方法...您的答案是否也相同?谢谢!
英文:
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!
答案1
得分: 0
- 404 Not found 是在请求的资源不可用时使用适当的。
- 405 Method not allowed 是在资源上不支持该 HTTP 方法时使用的(例如,对于只读资源,可能允许使用
GET
,但不允许使用POST
、PUT
或DELETE
)。
当用户未指定 subscriptionId
时,请求的资源将为 /customer-web/customers/{customerId}/subscription/
。该资源可能存在(并列出所有订阅),也可能不存在(未绑定到该资源),在这种情况下,适用**404 Not found
**。
英文:
- 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 (e.g. for read-only resources,
GET
may be allowed but notPOST
,PUT
orDELETE
).
When the user does not specify the subscriptionId
, the requested resource would be /customer-web/customers/{customerId}/subscription/
. <br>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.
答案2
得分: 0
是的,逻辑上是错误的。
假设您是一名客户,您拥有一些关于自己的数据/信息。这些数据保存在数据库或其他数据存储中。现在,您将被视为该服务的用户。现在,该服务提供一些功能,例如:他们可以为您提供有关您最喜欢的主题的最新消息。现在,您必须订阅该特定功能,以便您可以收到通知。但是,为了使自己收到通知,服务器首先必须将您订阅到该事件/功能。每个数据库/数据存储的表格行都有一个唯一标识符,例如行号。现在,他们必须搜索该ID/行号(将行号假定为您的用户ID)以检查您是否是适用的用户。
如果他们找到了您,那么他们将订阅您的身份验证器ID或用户ID到事件表的subscribedUser
列,并将该事件的订阅ID添加到您的行的subscribedEvents
列。然后,如果您订阅了,则他们会向您发送有关您最喜欢的主题或其他内容的通知。
在上述故事中,创建用户/客户订阅所需的事项包括:
- 客户ID/用户ID(这可以是该用户/客户的行索引号,将被推送到该事件的
subscribedUser
列) - 事件ID(这可以是该事件的行索引号,将用作subscriptionId,将被推送到该用户/客户的行的
subscribedEvent
列)
现在,如果上述ID之一丢失了,您将用什么搜索?如果您没有提供其中任何一个,那么可能会得到null
|undefined
,而行号或用户ID/事件ID不能为Null,因为它们是数字,而且是唯一的。因此,很容易猜测,查询操作将返回空[]
,甚至可能返回null
。而null是虚假值。因此,服务器肯定会将其视为错误,并作为404
的未找到
响应发送回去。
但有一点需要注意,如果customerId
用作URI的参数:parameter
,并且没有传递,那么URL将指向另一个终点,并返回其他/无关的响应。如果该终点不存在,则会返回另一个404错误。对subscriptionId
也是一样的。上述标准仅适用于错误的customerId
或subscriptionId
。现在,如果您不传递其中任何一个,则地址将是错误的,并且如果在该终点中不存在任何内容,则会发送404
,如果终点存在,则会返回其他响应。
英文:
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 e.g 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论