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

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

问题描述

我遇到以下情况: 我有一个API,可让我检索唯一资源:

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

GET myapi/resource_id

如果该资源不存在,我将返回404 HTTP状态代码.

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

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

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

GET myapi/resource_id1,resource_id2

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

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

  1. 200,错误码中带有JSON解释

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

206,它可以使用错误键中的JSON解释进行更明确的显示

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

400/404代码,但这不是很好,因为API仍会返回两个信息之一

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

另一种解决方案?

谢谢您的帮助.

推荐答案

我认为这里有2个选项.

I think there are 2 options here.

  • 发回200,甚至不要提及丢失的资源.
  • 以空着的身躯寄回404.

206用于范围请求,400用于格式错误的请求,因此此处均不适用.

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

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

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