RESTful API 设计.当请求带有过滤器时,您是否也应该返回该过滤器的值? [英] RESTful API design. When request comes with a filter should you return the values for that filter too?

查看:55
本文介绍了RESTful API 设计.当请求带有过滤器时,您是否也应该返回该过滤器的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设客户端对/items/color/{color} 进行了 GET 请求

Lets say client does GET request for /items/color/{color}

当服务器返回具有所述颜色的对象数组时,每个项目对象是否应该具有颜色属性?

When server returns array of objects that have said color, should each item object have color property or not?

客户端知道返回项目的颜色,因为他请求了那个颜色,所以服务器应该尝试缩小响应大小还是不响应?

Client knows the color of returned items because he requested that color, so should server try to make a response size smaller or no?

人们可以更多地讨论节省带宽部分吗?如果返回整个资源更好,答案是否可以包括为什么返回整个资源比节省带宽更好,而不仅仅是为什么应该返回整个资源.

Can people touch more on saving bandwidth part? If it's better to return whole resource, can the answers include why it's better to return whole resource versus saving bandwidth, instead of just why whole resource should be returned.

推荐答案

总的来说(至少我理解的 REST 是这样的),请求的结果应该总是完整的资源.如果项目包含成员 color,则没有理由在结果中隐藏该成员.这将与 REST 的资源概念相矛盾.资源不会改变其属性.

In general (at least that's the idea of REST as I understand it), the result for the request should always be the complete resource. If the item contains a member color, there's no reason to suppress that member in the result. That would contradict the concept of resources of REST. A resource doesn't change its properties.

压制成员不仅会出乎意料,甚至可能会在客户端真正期望该成员时破坏它.

Suppressing members would not only come unexpected, it might even break the client when it actually expects that member.

假设客户端具有无需过滤器即可解析 REST 调用结果的功能.将返回所有字段,客户端将解析所有字段.现在客户端请求完全相同的资源(item),但字段突然不同——上面解析结果的代码不能重用.

Lets assume the client has functionality to parse the result of your REST call without a filter. All fields would be returned and the client would parse all the fields. Now the client requests the exact same resources (item), but suddenly the fields are different - the code from above to parse the result can not be reused.

另外,当你想到它时,压制该成员可能比仅仅返回它更多的工作.

Also, when you think about it, it's probably more work to suppress that member than to just return it.

这篇关于RESTful API 设计.当请求带有过滤器时,您是否也应该返回该过滤器的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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