REST API - 部分响应的 HTTP 状态 [英] REST API - HTTP status for a partial response

查看:29
本文介绍了REST API - 部分响应的 HTTP 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下情况:我有一个允许我检索唯一资源的 API:

GET myapi/resource_id

如果这个资源不存在,我会返回一个 404 HTTP 状态码.

此 API 还允许通过同一请求检索多个资源:

GET myapi/resource_id1,resource_id2

如果两个资源之一不存在,我应该发回哪个 HTTP 状态代码?

<块引用>

  1. 200 错误键中的 JSON 解释

  2. 206 允许在错误键中的 JSON 中进行更明确的解释

  3. 400/404 代码,但这不好,因为 API 仍然返回两个信息之一

  4. 另一种解决方案?

感谢您的帮助.

解决方案

我认为这里有 2 个选项.

  • 发回 200,甚至不要提及丢失的资源.
  • 返回带有空正文的 404.

206 用于范围请求,400 用于格式错误的请求,因此这些都不适用.

I encounter the following case: I have an API that allows me to retrieve a unique resource:

GET myapi/resource_id

If this resource does not exist, I return a 404 HTTP status code.

This API also allows to retrieve several resources via the same request:

GET myapi/resource_id1,resource_id2

Which HTTP status code should I send back if one of the two resources does not exist?

  1. 200 with an explanation in the JSON in an error key

  2. 206 which allows to be more explicit with an explanation in the JSON in an error key

  3. 400 / 404 code but this is not fine because the API still returns one of the two information

  4. another solution?

Thank you for your help.

解决方案

I think there are 2 options here.

  • Send back 200 and don't even mention the missing resource.
  • Send back 404 with an empty body.

The 206 is for range requests, the 400 is for malformed requests, so none of those apply here.

这篇关于REST API - 部分响应的 HTTP 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆