REST API 框架.无效查询字符串参数的推荐行为 [英] REST API Framework. Recommended behavior for invalid querystring parameter

查看:21
本文介绍了REST API 框架.无效查询字符串参数的推荐行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个 REST API 框架,我想知道当客户端提交无效的查询字符串参数时推荐的行为是什么.

I am implementing a REST API Framework, and I wonder what the recommendedbehavior is, when a client submits an invalid querystring parameter.

我会用一个具体的例子来说明我的意思:比如说,我在/api/contacts/端点上有一个 API 处理程序,该处理程序提供了一个名为 id 的查询字符串过滤器,它使客户端能够选择具有提供的 ID 的某些联系人.

I will illustrate what I mean with a specific example: Say, I have an API handler on the /api/contacts/ endpoint, and the handler provides a querystring filter named id, which enables clients to select certain contacts with the provided IDs.

因此,GET 或 DELETE 请求可以是 /api/contacts/?id=2&id=4&id=lalalala.

So, a GET or DELETE request could be /api/contacts/?id=2&id=4&id=lalalala.

显然,没有id=lalalala 的联系人.在这种情况下,服务器应该怎么做?

Clearly, there is no such thing as a Contact with id=lalalala. In this case, what should the server behave like?

  • 忽略带有 id=lalalala 的无效联系人,只过滤有效 ID 2 和 4 上的联系人.

  • Ignore the invalid Contact with id=lalalala, and only filter the contacts on the valid ids, 2 and 4.

使用指示此错误的错误代码进行响应.如果是,应提供哪个错误代码?

Respond with an error code that indicates this error. If yes, which error code should be provided?

提前致谢.

澄清;我开发的框架的主要重点是具有可预测的行为以及响应代码.出于这个原因,我希望使用基于此框架构建的 API 的客户能够期待最少的惊喜.所以,问题基本上是:在这种情况下,API 是否应该返回错误(如果是,是哪个)?还是忽略无效的过滤器条目,只过滤正确的查询字符串参数?

To clarify; The main focus of the framework I develop, is having a predictable behavior and hence response codes. For this reason, I want the clients consuming an API built on this framework, to expect the least possible surprises. So, the question basically is: Should the API return an error in this case(and if yes, which)? Or ignore invalid filter entries, and only filter on the correct querystring parameters?

推荐答案

由于这是一个 REST 调用,我们谈论的是资源.每当我们有错误的过滤器时,我们应该返回正确的错误代码.

Since this is a REST call, we are talking about resources. And whenever we have a wrong filter, we should return a proper error code.

在这种情况下,我会选择 400 - bad request,因为找到并正确映射了资源 (/api/contacts),但是查询字符串部分.因此是 400 而不是 404.

In this case i would go for 400 - bad request as the resource was found and correctly mapped (/api/contacts), but there was a problem with the query string part. Therefore a 400 and not a 404.

如果有人请求 /api/contacts-all 或一些不存在的资源,将返回 404.

Would return a 404 if someone requested /api/contacts-all or some non-existant resource.

根据以下评论进行编辑

同意你的评论.理想情况下,400 是请求的问题.按照这个,你可以使用 422 Unprocessable Entity.请查看下面的 stackoverflow 链接,它讨论了同样的事情.

Agree to your comment. Ideally a 400 is a problem with the request. Going by that, you could use a 422 Unprocessable Entity. Please look at the stackoverflow link below and it talks about the same thing.

我猜世界各地的开发人员会更愿意看到 400 而不是 422,因为大公司都在使用 400 而不是 422.

I would guess that developers around the world would be more comfortable seeing a 400 than 422 for such logical errors due to the fact that bigger companies are using 400 and not 422.

参考资料:Http 状态代码400 表示逻辑错误与格式错误的请求

这篇关于REST API 框架.无效查询字符串参数的推荐行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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