I don't want to see so long parameters string in the URI. So, can GET method use json data?

在我的情况下,我需要过滤给定参数的结果.如果有很多参数,长度可能会超过URI的限制.那么,有解决这个问题的最佳实践吗?

推荐答案

In theory, there's nothing preventing you from sending a request body in a GET request. The HTTP protocol allows it, but have no defined semantics, so it's up to you to document what exactly is going to happen when a client sends a GET payload. For instance, you have to define if parameters in a JSON body are equivalent to querystring parameters or something else entirely.

However, since there are no clearly defined semantics, you have no guarantee that implementations between your application and the client will respect it. A server or proxy might reject the whole request, or ignore the body, or anything else. The REST way to deal with broken implementations is to circumvent it in a way that's decoupled from your application, so I'd say you have two options that can be considered best practices.

The simple option is to use POST instead of GET as recommended by other answers. Since POST is not standardized by HTTP, you'll have to document how exactly that's supposed to work.

我更喜欢的另一种 Select 是,假设GET有效负载从未被篡改,则实现应用程序.然后,如果有一个坏的实现,您可以允许客户机用X-HTTP-Method-Override覆盖HTTP方法,这是客户机用POST模拟HTTP方法的常用约定.因此,如果一个客户机有一个中断的实现,它可以将GET请求写为POST,发送X-HTTP-Method-Override: GET方法,您可以拥有一个与应用程序实现解耦的中间件,并相应地重写该方法.如果你是一个纯粹主义者,这是最好的 Select .

Json相关问答推荐

如何使用Laravel在MariaDB JSON kolumn中使用unicode字符

Vega-Lite时钟(使用Vega-Lite中的计时器)

GO KaZaam转换返回意外结果

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

使用 jolt 变换展平嵌套 JSON - 非常复杂

使用 jq 如何更改键的值?

展平多个数组以保持顺序

在Databricks中如何将JSON文件作为字典读取

使用 map_values Select 包含空格的字符串未按预期工作

如何在不使用 Newtonsoft.JSON 的情况下序列化/反序列化

Shell脚本空格转义

Powershell - 如何从 JSON 中删除/过滤元素但保留其余部分?

使用带有逗号的字段名称构建 struct

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

如何从 JSON 对象中获取日期

如何为名称/值 struct 创建 JSON 模式?

Jackson 中的 readValue 和 readTree:何时使用哪个?

什么 Python 框架用于没有前端的 REST/JSON Web 服务?

使用 Retrofit 解析动态密钥 Json 字符串

从 Json 对象 Android 中获取字符串值