对于 Restful API,GET 方法可以使用 json 数据吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29571284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:50:10  来源:igfitidea点击:

For Restful API, can GET method use json data?

jsonapirestget

提问by HappyLiang

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

我不想在 URI 中看到这么长的参数字符串。那么,GET方法可以使用json数据吗?

In my situation, I need to filter the result given kind of parameters. If there are a lot of parameter, the length may exceed the limit of URI. So, is there best practice for this problem?

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

回答by Pedro Werneck

In theory, there's nothing preventing you from sending a request body in a GETrequest. 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 GETpayload. For instance, you have to define if parameters in a JSON body are equivalent to querystring parameters or something else entirely.

理论上,没有什么可以阻止您在请求中发送请求正文GET。HTTP 协议允许这样做,但没有定义的语义,因此由您来记录客户端发送GET有效负载时究竟会发生什么。例如,您必须定义 JSON 正文中的参数是否等同于查询字符串参数或其他完全相同的参数。

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.

但是,由于没有明确定义的语义,您不能保证您的应用程序和客户端之间的实现会尊重它。服务器或代理可能会拒绝整个请求,或忽略正文或其他任何内容。处理损坏的实现的 REST 方法是以与您的应用程序分离的方式规避它,所以我认为您有两个可以被视为最佳实践的选项。

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

简单的选择是使用POST而不是GET按照其他答案的建议使用。由于POSTHTTP 没有标准化,因此您必须记录它应该如何工作。

Another option, which I prefer, is to implement your application assuming the GETpayload is never tampered with. Then, in case something has a broken implementation, you allow clients to override the HTTP method with the X-HTTP-Method-Override, which is a popular convention for clients to emulate HTTP methods with POST. So, if a client has a broken implementation, it can write the GETrequest as a POST, sending the X-HTTP-Method-Override: GETmethod, and you can have a middleware that's decoupled from your application implementation and rewrites the method accordingly. This is the best option if you're a purist.

我更喜欢的另一种选择是假设GET有效负载从未被篡改来实现您的应用程序。然后,如果某些东西的实现有问题,您允许客户端使用 覆盖 HTTP 方法X-HTTP-Method-Override,这是客户端使用 模拟 HTTP 方法的流行约定POST。因此,如果客户端有一个损坏的实现,它可以将GET请求编写为POST,发送X-HTTP-Method-Override: GET方法,并且您可以拥有一个与应用程序实现分离并相应地重写方法的中间件。如果你是一个纯粹主义者,这是最好的选择。

回答by jfrattarola

To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode). However, considering your reason for doing this is due to the length of the URI, using JSON will be self-defeating (introducing more characters than required).

要回答您的问题,是的,您可以在 URI 中传递 JSON 作为 GET 请求的一部分(前提是您进行了 URL 编码)。但是,考虑到您这样做的原因是由于 URI 的长度,使用 JSON 将弄巧成拙(引入的字符数超过所需)。

I suggest you send your parameters in body of a POST request, either in regular CGI style (param1=val1&param2=val2) or JSON (parsed by your API upon receipt)

我建议您在 POST 请求的正文中发送参数,以常规 CGI 样式 ( param1=val1&param2=val2) 或 JSON(在收到时由您的 API 解析)