java 将许多请求参数与 GET 请求放在 REST API 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16557638/
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-10-31 23:18:02  来源:igfitidea点击:

Put many request parameters with GET request in REST API

javahttprest

提问by Madhusudan Joshi

Is it a good practice to put many request parameters with GET request in REST API?

将许多请求参数与 GET 请求放在 REST API 中是一个好习惯吗?

I have gone through couple of sites and tried to get the standard way of doing GET URIwith parameters implementation.

我浏览了几个网站,并试图获得GET URI处理参数实现的标准方法。

Here's what i am trying to achieve : search for all users having provided all the search criteria.

这就是我想要实现的目标:search for all users having provided all the search criteria

Search criteria are like companyID, sections, offset, limit, orderby, filter.

搜索条件类似于companyIDsectionsoffsetlimitorderbyfilter

As per basic standards GETrequest can not have request body or payloadin it.

按照基本标准GET要求不能包含request body or payload在其中。

Will it be good way to make a GETrequest and put all parameters behind ?in the request URL, something like this :

发出GET请求并将所有参数放在?请求 URL 中是否是一种好方法,如下所示:

GET http://localhost:8080/api/users?companyId=qwerty&sections=hr&offset=0&limit=20&oorderby=asc&filter=^[sSmM]

GET http://localhost:8080/api/users?companyId=qwerty&sections=hr&offset=0&limit=20&oorderby=asc&filter=^[sSmM]

I was thinking to make a PUTor POSTrequest and send all these data in payload, and implement the code to return the desired response, i.e list of users.

我正在考虑发出一个PUTPOST请求并在有效载荷中发送所有这些数据,并实现代码以返回所需的响应,即用户列表。

If i do this i am changing the default behavior of HTTP methods.

如果我这样做,我将更改HTTP methods.

Can you please guide me a way to get out of the situation. Thanks.

你能指导我摆脱困境的方法吗?谢谢。

回答by MatthiasLaug

I think it does not matter how many parameters you put in a GETrequest. The purpose of a POSTor PUTrequest is not to keep your URL clean, but to aline with a REST structure.

我认为在GET请求中放入多少参数并不重要。POSTorPUT请求的目的不是保持您的 URL 干净,而是与 REST 结构保持一致。

the raw definition of GET, POST and PUT are

GET、POST 和 PUT 的原始定义是

  • GET: get a resource from your server
  • POST: create a resource on your server
  • PUT: update a resource on your server
  • GET: 从你的服务器获取资源
  • POST: 在你的服务器上创建一个资源
  • PUT:更新服务器上的资源

回答by Jilles van Gurp

Actually you can pass a request body with a GET request. It's just not very common to do it. Jquery supports this on the browser side and REST APIs such as for example the elastic search API use this. They have a nice json based query dsl and you can actually use a GET request for these. Because some HTTP frameworks don't support passing a body with a GET, Elasticsearch offers a fallback with POST as well.

实际上,您可以使用 GET 请求传递请求正文。这样做并不常见。Jquery 在浏览器端支持此功能,而 REST API(例如弹性搜索 API)使用此功能。他们有一个很好的基于 json 的查询 dsl,您实际上可以对这些使用 GET 请求。由于某些 HTTP 框架不支持使用 GET 传递主体,因此 Elasticsearch 也提供了 POST 回退。

As for using lots of url parameters, there are two issues you should keep in mind:

对于使用大量 url 参数,您应该记住两个问题:

  1. URLs don't have unlimited length. There are some differences between browsers for this but typically it is a few KB maximum. Particularly some older mobile browsers have limitations here. It's pretty easy to run into this limit if you put a lot of things in the request and basically this means urls get truncated.
  2. Long URIs look kind of ugly and you wouldn't necessarily want to show them in a web UI.
  1. URL 没有无限长度。浏览器之间对此存在一些差异,但通常最大为几 KB。特别是一些较旧的移动浏览器在这方面有局限性。如果您在请求中放入很多东西,很容易遇到这个限制,基本上这意味着 url 会被截断。
  2. 长 URI 看起来有点难看,您不一定希望在 Web UI 中显示它们。

回答by madhairsilence

Rest is nothing but a technique/framework/procedure.

休息只不过是一种技术/框架/程序。

It is no way special from a simple servlet call. It always follows the standard convention of request

简单的 servlet 调用并没有什么特别之处。它始终遵循请求的标准约定

GET- Retrieve any data from the Server. You can have any number of parameters with a limit of 256 Characters. The parameters should just act as a filter for the incoming data and they are not suppose to alter the system.

GET- 从服务器检索任何数据。您可以拥有任意数量的参数,限制为 256 个字符。这些参数应该只是作为传入数据的过滤器,它们不会改变系统。

https://www.google.co.in/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

https://www.google.co.in/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

If you see the above URL, u can see many parameters. But they are just filters to the results. They does not alter the system.

如果你看到上面的 URL,你可以看到很多参数。但它们只是结果的过滤器。他们不会改变系统。

POST- Send some data such that it alters the system, usually add/remove data.

POST- 发送一些数据以改变系统,通常是添加/删除数据。

PUT- This is something special, say an advancement of POST, to be used when you want to update the exisiting data

PUT- 这是一些特别的东西,比如 POST 的进步,当你想更新现有数据时使用