I don't want to see so long parameters string in the URI. So, can GET method use json data?
在我的情况下,我需要过滤给定参数的结果.如果有很多参数,长度可能会超过URI的限制.那么,有解决这个问题的最佳实践吗?
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 .