缺少父资源的REST API代码/消息 [英] REST API code/message for missing parent resource

查看:86
本文介绍了缺少父资源的REST API代码/消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关正确响应代码的指南.在请求构成另一资源一部分的资源时出现错误消息.

I'm looking for some guidance for the correct response code & message when requesting for a resource that forms part of another resource.

例如,以下位置的GET请求:

For example, a GET request on:

用户/{id}

将返回404,并显示一条未找到用户资源的消息.

where the user does not exist would return a 404, with a message of user resource not found.

我的问题是,当找不到用户资源时,以下内容应返回什么?

My question is, what should the following return when no user resource is found:

用户/{id}/朋友

我目前正在返回与第一个示例相同的代码/消息.我是否应该返回一条专门与好友资源有关的消息?我个人认为,如果您有更大的URI链,让API客户端意识到找不到父资源会更有用.

I'm currently returning the same code/message as in the first example. Should I be returning a message relating specifically to the friends resource? I personally think it's more helpful to make the API client aware that the parent resource isn't found, incase you have a larger URI chain.

推荐答案

在此特定示例中,如果要点是让客户端区分对不存在用户的好友请求和对不存在用户的好友请求,根本没有朋友,我认为在第一种情况下返回404最为合理,在第二种情况下返回200则为空.

In this particular example, if the point is to let the client differentiate between a friends request for a non-existent user, and a friends request for a user who simply has no friends, I think it would make the most sense to return 404 in the first case, and 200 with an empty set in the second.

换句话说,无"对朋友来说是有效值.在任何情况下都不会存在用户,但他们的(可能为空)朋友列表不存在,因此在为父资源发布404时永远不会有任何歧义.

In other words, "none" is a valid value for friends. There's no case where a user exists but their (potentially empty) list of friends doesn't, so there's never any ambiguity in issuing a 404 for the parent resource.

这篇关于缺少父资源的REST API代码/消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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