REST 资源 url 中的查询字符串 [英] Querystring in REST Resource url

查看:47
本文介绍了REST 资源 url 中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天与一位同事讨论了在 REST URL 中使用查询字符串的问题.以这两个例子为例:

I had a discussion with a colleague today around using query strings in REST URLs. Take these 2 examples:

1. http://localhost/findbyproductcode/4xxheua
2. http://localhost/findbyproductcode?productcode=4xxheua

我的立场是 URL 应该像示例 1 那样设计.这更清晰,我认为在 REST 中是正确的.在我看来,如果产品代码不存在,从示例 1 返回 404 错误是完全正确的,而示例 2 返回 404 将是错误的,因为页面应该存在.他的立场是这并不重要,他们都做同样的事情.

My stance was the URLs should be designed as in example 1. This is cleaner and what I think is correct within REST. In my eyes you would be completely correct to return a 404 error from example 1 if the product code did not exist whereas with example 2 returning a 404 would be wrong as the page should exist. His stance was it didn't really matter and that they both do the same thing.

由于我们都没有找到具体的证据(诚然,我的搜索范围并不广泛),我想知道其他人对此的看法.

As neither of us were able to find concrete evidence (admittedly my search was not extensive) I would like to know other people's opinions on this.

推荐答案

在典型的 REST API 中,示例 #1 更正确.资源表示为 URI,而#1 的作用更大.当找不到产品代码时返回 404 绝对是正确的行为.话虽如此,我会稍微修改 #1 以使其更具表现力:

In typical REST API's, example #1 is more correct. Resources are represented as URI and #1 does that more. Returning a 404 when the product code is not found is absolutely the correct behavior. Having said that, I would modify #1 slightly to be a little more expressive like this:

http://localhost/products/code/4xheaua

查看其他精心设计的 REST API - 例如,查看 StackOverflow.你有:

Look at other well-designed REST APIs - for example, look at StackOverflow. You have:

stackoverflow.com/questions
stackoverflow.com/questions/tagged/rest
stackoverflow.com/questions/3821663

这些都是处理问题"的不同方式.

These are all different ways of getting at "questions".

这篇关于REST 资源 url 中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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