简体   繁体   English

REST API设计:使用GET进行复杂的查询

[英]REST API design: complex query with GET

I am designing a REST call that should deliver information for a location (lat/lon) and consider the user context/configuration. 我正在设计一个REST调用,该调用应传递位置(经纬度)的信息并考虑用户上下文/配置。

As the number of user properties is high and nested, I am not sure, what is the correct way to design a new query (GET vs POST). 由于用户属性的数量很多且很嵌套,因此我不确定设计新查询(GET与POST)的正确方法是什么。 Currently we use a POST request for simplicity - the query payload could look like this, but is custom and very different for each user. 当前,为了简化起见,我们使用POST请求-查询有效负载可能看起来像这样,但是是自定义的,并且对于每个用户而言都非常不同。 It also includes an array of multiple configuration items. 它还包括多个配置项的数组。 Currently the request looks like this: 当前,请求如下所示:

POST http://api.something.com/locationInformation POST http://api.something.com/locationInformation

{
  "location": {
    "accuracy": 30,
    "coordinates": [
      16.34879820048809,
      48.230067741347334
    ],
    "provider": "network",
    "timestamp": "2016-01-06T12:00:00.000Z"
  },
  "userConfiguration": [
    {
      "id": "asdfasdfasdfs09898sdf",
      "values": [
        "false"
      ]
    },
    {
      "id": "iojkljio230909sdjklsdf",
      "values": [
        "99jkjiouio89",
        "sdfilkjöjfoi093s09sdf"
      ]
    }
  ]
}

So my question is: is it in such a case ok to "abuse" a POST request in order to query information? 所以我的问题是:在这种情况下可以“滥用” POST请求以查询信息吗?

Is there an elegant way to pass such data using a GET request? 有没有一种优雅的方法可以使用GET请求传递此类数据?

Yes u can pass this data using a GET request by passing it to a request header. 是的,您可以通过将GET数据传递到请求标头来传递此数据。 use the header() method.Initialize a String variable say String data=//your json; 使用header()方法初始化一个String变量,例如String data=//your json; and pass it to the header as follows header("data",data) in your client while building the request. 并在建立请求时将其传递给客户端中的头文件,如下所示: header("data",data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM