REST API - HTTP status for a partial response

最后都变了- 提交于 2021-01-29 07:23:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/54673720/rest-api-http-status-for-a-partial-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!