简体   繁体   English

当使用者要求与不存在的资源相关的资源集合时,我应该发送什么API响应?

[英]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时:

  • If the provider exists and has some publications, they get 200 OK with the collection of publications. 如果提供者存在并且有一些出版物,则他们可以获得200 OK的出版物集合。
  • If the provider exists and has no publications, they get 200 OK with a collection that is empty. 如果提供者存在并且没有出版物,则他们获得200 OK,并且其集合为空。

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.

相关问题 找不到资源的 API 响应应该是什么 - What should be the API response for a resource not found 当资源收到 DELETE 请求时,相关资源会发生什么情况? - What happens to related resources when a resource receives a DELETE request? 如果资源不存在,我的 API 应该使用什么 HTTP 代码 - What HTTP code should my API use if the resource does not exist 在我的 REST API 中创建具有已知 ID 的资源时,我应该发布到集合吗? - Should I POST to the collection when creating a resource with a known ID in my REST API? 我可以将数组(不是集合)发送到Laravel API资源以转换数据吗? - Can I send an array (not a collection) to a Laravel API Resource to transform the data? 设计一个 RESTful api 用于一次创建资源及其相关资源 - Design a RESTful api for creating a resource and its related resources at once Laravel-如果找不到或匹配API路由,应该怎么办? - Laravel - What should happen when API route doesn't found or match? 当相关资源处于阻塞状态时的REST API响应错误代码 - Rest API Response Error Code for when a related resource has a blocked state rest api的响应是否应该包含资源的ID? - Should response of rest api contain ID of a resource? RESTful API:为可以访问其他资源的资源集合建模 - RESTful API: Modelling a collection of resources that have access to another resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM