当提供无效的查询参数时,REST API return 是否应该返回 4xx 响应?
Should a REST API return a 4xx response when an invalid query parameter is supplied?
考虑接受 GET 请求以列出项目的 RESTful API:
GET /1.0/items/
>> {"items": [{}, {}, ..., {}]} # All items returned
现在考虑每个项目都有一个颜色字段,我可以过滤我的项目:
GET /1.0/items?color=blue
>> {"items": [{}, {}, ..., {}]} # Only blue items returned
如果我的 API 收到无效查询 参数 (不是有效查询参数上的无效值):
GET /1.0/items?notvalid=blue
预期的行为应该是什么?我的 API return 应该一个 4xx
响应通知客户端请求无效,还是 API 应该像没有提供过滤器参数一样执行项目列表?
根据 JSON API 文档:
In most cases, JSON API requires the server to return an error when it encounters an invalid value for a JSON API–defined query parameter. However, for API-specific query parameters (i.e. those not defined by JSON API), a server may choose to ignore an invalid parameter and have the request succeed, rather than respond with an error.
这是我经常在 API 上看到的行为。
Should my API return a 4xx response informing the client that the request was invalid, or should the API perform the listing of the items as if no filter parameter was supplied?
/1.0/items?notvalid=blue
标识一个资源。这个标识符可以解释为一个分层部分和一个查询(参见RFC 3986, section 3),但标识符是整个事情。面对不存在的资源的 URI,文档存储将以 404 错误响应。所以这种行为是完全可以接受的(也可以使用更普遍的 400 错误,但这并不常见)。
另一种有优点的方法是使用 must ignore policy. Treating the URI as a x-www-form-urlencoded expression of key-value pairs, one can liberally accept 查询,忽略无法识别的键,并为缺少的任何键提供默认值。
采用这种方法,此标识符将被视为已被拼写 /1.0/items?
这为您提供了一些防止更改的保护(客户端和服务器不需要完全同意才能取得进展)。
注意:在 REST 中——客户端通常会使用通过协议引导它的超媒体表示;因此,客户端将通过表单或 uri 模板发现哪些参数需要作为查询字符串的一部分。这实际上只是相同的 must-ignore 语义,但应用在不同的地方。
should the API perform the listing of the items as if no filter parameter was supplied?
您可能想要明确识别您正在 returning 的引用,以便客户端可以检测到差异;例如,通过将请求重定向到您要访问的名称 return,或者通过 returning a Content-Location header.
考虑接受 GET 请求以列出项目的 RESTful API:
GET /1.0/items/
>> {"items": [{}, {}, ..., {}]} # All items returned
现在考虑每个项目都有一个颜色字段,我可以过滤我的项目:
GET /1.0/items?color=blue
>> {"items": [{}, {}, ..., {}]} # Only blue items returned
如果我的 API 收到无效查询 参数 (不是有效查询参数上的无效值):
GET /1.0/items?notvalid=blue
预期的行为应该是什么?我的 API return 应该一个 4xx
响应通知客户端请求无效,还是 API 应该像没有提供过滤器参数一样执行项目列表?
根据 JSON API 文档:
In most cases, JSON API requires the server to return an error when it encounters an invalid value for a JSON API–defined query parameter. However, for API-specific query parameters (i.e. those not defined by JSON API), a server may choose to ignore an invalid parameter and have the request succeed, rather than respond with an error.
这是我经常在 API 上看到的行为。
Should my API return a 4xx response informing the client that the request was invalid, or should the API perform the listing of the items as if no filter parameter was supplied?
/1.0/items?notvalid=blue
标识一个资源。这个标识符可以解释为一个分层部分和一个查询(参见RFC 3986, section 3),但标识符是整个事情。面对不存在的资源的 URI,文档存储将以 404 错误响应。所以这种行为是完全可以接受的(也可以使用更普遍的 400 错误,但这并不常见)。
另一种有优点的方法是使用 must ignore policy. Treating the URI as a x-www-form-urlencoded expression of key-value pairs, one can liberally accept 查询,忽略无法识别的键,并为缺少的任何键提供默认值。
采用这种方法,此标识符将被视为已被拼写 /1.0/items?
这为您提供了一些防止更改的保护(客户端和服务器不需要完全同意才能取得进展)。
注意:在 REST 中——客户端通常会使用通过协议引导它的超媒体表示;因此,客户端将通过表单或 uri 模板发现哪些参数需要作为查询字符串的一部分。这实际上只是相同的 must-ignore 语义,但应用在不同的地方。
should the API perform the listing of the items as if no filter parameter was supplied?
您可能想要明确识别您正在 returning 的引用,以便客户端可以检测到差异;例如,通过将请求重定向到您要访问的名称 return,或者通过 returning a Content-Location header.