[英]What API response should I send when a consumer asks for a collection of resources related to a resource that doesn't exist?
Let's say I have a resource 假设我有资源
/providers/123
When a consumer sends a GET for that, it receives 200 OK and a record for the provider identified by 123, or a 404 Not Found if there is no provider with id 123. Fine. 当使用者为此发送GET时,它将收到200 OK和由123标识的提供者的记录,如果没有ID为123的提供者,则显示404 Not Found。
Now let's say I have another resource 现在说我还有另一个资源
/providers/123/publications
which represents a collection of publications authored by provider 123. When a consumer sends a GET for that: 它表示由提供商123创作的出版物的集合。当消费者为此发送GET时:
But what if the provider doesn't exist? 但是如果提供者不存在怎么办? 404?
404? 200 with empty set?
200空套? 200 with empty set and a warning?
200带有空集和警告? Something else?
还有吗
404 for sure. 肯定是404。 The URL refers to a resource that does not exist.
URL指的是不存在的资源。
Don't give any more information than is required. 不要提供超出要求的更多信息。
Depends on your end user (the entity making the API calls) 取决于您的最终用户(进行API调用的实体)
If its a human being then 200 with an error saying that the provider doesnt exist seems the best. 如果它是人类,那么最好说200个错误,说提供者不存在。 This case is applicable mostly with single calls to your API
这种情况主要适用于对API的单次调用
If the end user is a robot parsing data from your resultset then 200 with an empty set will be a good choice.This case is applicable when API calls are made in bulk. 如果最终用户是机器人,它会从您的结果集中解析数据,那么200个空集将是一个不错的选择。这种情况适用于批量进行API调用的情况。
Example: This Geonames API gives country code from latitude and longitude, but if I have invalid latitude and longitude, it will throw an error message 示例:此地名API提供了纬度和经度的国家/地区代码,但是如果我的纬度和经度无效,则会抛出错误消息
http://api.geonames.org/countryCode?lat=181&lng=181&username=demo http://api.geonames.org/countryCode?lat=181&lng=181&username=demo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.