REST:映射 404 HTTP 状态代码 [英] REST: Mapping 404 HTTP Status codes

查看:56
本文介绍了REST:映射 404 HTTP 状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的团队正在开发 RESTFul 应用程序……我们正在讨论最佳实践"方法.

Our team is developing RESTFul applications...we are debating the "BEST PRACTICE" approach.

是否应该为类似过滤器的查询返回 404 状态代码响应?假设我的 GET URL 是

Should 404 status code response be returned for a filter-like query? Say my GET URL is

.../1/service/startsWith/a.json

.../1/service/startsWith/a.json

并且它返回我的数据库中以 A 开头的所有值...但是如果没有找到a"值,我是否应该只返回带有空 json 字符串的状态代码 200?或状态码 404.

and it returns all values that start with A in my database...but if no "a" values are found should i just return status code 200 with an empty json string? or status code 404.

谢谢!

推荐答案

参见 这个问题,在对我的回答的更新中,我解决了您的问题.特别是这一点,

See this question, in the update to my answer I address your issue. Specifically this bit,

我认为是否要的答案返回 404 取决于是什么正在检索的资源.是它是搜索的表示结果,或者它是一个的表示产品?要知道这一点,你真的需要查看导致的链接关系网址.

I think the answer to whether to return 404 depends on the what is the resource that is being retrieved. Is it a representation of a search result, or is it a representation of a product? To know this you really need to look at the link relation that led us to the URL.

如果 URL 应该返回一个产品表示然后是 404如果代码正确,则应返回不存在.如果 URL 返回搜索结果则不应返回 404.

If the URL is supposed to return a Product representation then a 404 should be returned if the code does not exist. If the URL returns a search result then it shouldn't return a 404.

最终的结果就是那个 URL看起来不是决定性的因素.话虽如此,它是使用查询字符串的约定返回搜索结果所以它更多直观地使用该样式的 URL当您不想返回 404 时.

The end result is that what the URL looks like is not the determining factor. Having said that, it is convention that query strings are used to return search results so it is more intuitive to use that style of URL when you don't want to return 404s.

这篇关于REST:映射 404 HTTP 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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