rest - HTTP 状态,以防我不想支持一个或多个字段的搜索

标签 rest http http-status-codes

我已成功实现 POST/ordersGET/orders/{id} API。现在我想创建一个搜索 API。

我的资源具有以下字段:customerId、orderId、customerName、orderName 和 orderType。

我想根据资源中存在的字段再创建一个获取 API。因此,我将其设计为: GET/orders?orderType=ABC&customerName=Xyz

但我想限制某些参数的搜索。在上述情况下,假设我想限制对 orderName 的搜索(尽管该字段确实存在)。

现在,如果您点击GET/orders?orderName=DEF,我将回复不支持搜索。

在这种情况下,HTTP 状态代码应该是什么?

我担心使用 501,因为这与 HTTP 状态本身更相关。 422 也是如此,它与请求代码中传入的实体相关。无法发送 404,因为数据确实存在。需要澄清这些。

我最终应该实现什么?

最佳答案

Now if you hit GET /orders?orderName=DEF I will respond back with search not supported.

What should the HTTP status code be in this case?

404 Not Found

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

换句话说,状态代码专门将 target-uri 标识为请求中有问题的部分。

5xx 是不可能的,因为这是一个可以通过更改请求来解决的问题。

Can't send 404 as data does exist.

当然可以。

需要认识到的重要一点是,HTTP 状态代码是 transport documents over a network 的一部分。域,而不是您的应用程序域。

对于通过网络域传输文档,404 表示“您请求的标识符没有与之关联的文档”。这正是您所遇到的情况(从通用 HTTP 组件的角度来看)。

HTTP 的统一接口(interface)是文档存储的接口(interface);传递一个 key ,接收使用该 key 存储的文档。当我们报告错误时,我们使用与文档存储相同的语义来报告它们。 KEY_NOT_FOUND,在 HTTP 中是 404。

Can I send 404 with the message kind of - 'Intentionally not supported' or something?

是的,你可以做到。

该机制在RFC 7231中描述。

The 4xx (Client Error) class of status code indicates that the client seems to have erred. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

换句话说,您在响应正文中提供了问题的描述。 404 状态代码是元数据,它告诉通用组件(如浏览器或网络缓存),响应中包含的实体是一个错误,而不是资源的表示。 p>

例如,您可以使用 problem details 来解释错误情况文档。

关于rest - HTTP 状态,以防我不想支持一个或多个字段的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62872234/

相关文章:

javascript - 没有显示值

mysql - XWiki + MySQL 数据库 : Errors

javascript - Jasmine angular - 模拟http请求

java - 如果 http 请求的内容类型是 urlencoded,如何让 Spring Boot Controller 读取对对象的 http 请求正文?

http - 为错误的协议(protocol)返回什么 HTTP 代码

c# - 如何检测一个 URL 是否重定向到另一个 URL?

rest - Symfony Restful API 身份验证和 OAuth2

security - Elasticsearch 安全措施?

api - 验证客户端和保护 RESTful api 的通用方法

asp-classic - 如何在经典 ASP 中使用 HTTP 子状态发送 HTTP 错误代码?