0

I need to send a complex JSON document from the client application(AngularJS) to my server side(Java, Spring MVC/Rest) in order to retrieve the desired information.

This is the JSON example:

[
   {
      "operator":"AND",
      "queries":[
         {
            "value":10,
            "comparisonOperator":"\u003e\u003d",
            "characteristicId":391
         },
         {
            "value":50,
            "comparisonOperator":"\u003c\u003d",
            "characteristicId":391
         }
      ],
      "characteristicId":391
   },
   {
      "value":true,
      "comparisonOperator":"\u003d",
      "characteristicId":383
   }
]

My client app communicates with the back end via RESTful web services. For data retrieving I use the GET method and use url with path/query parameters.

I'm in doubt how to handle the case where I have to GET data and provide JSON document presented above.

Q:Is it okay to include such kind of JSON into GET request body ? If no, what is the best way to solve this issue ?

Anyway, I can't change this JSONwith path/query parameters due to schema less nature of this document.

acornagl
  • 521
  • 5
  • 24
alexanoid
  • 24,051
  • 54
  • 210
  • 410

5 Answers5

3

I wouldn't send the JSON in the GET payload. For GET requests, the payload doesn't have defined semantics and some servers might refuse the request. To support it, here's a quote from the RFC 7231, the current reference for the semantics and content of the HTTP/1.1 protocol:

4.3.1. GET

[...]

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

[...]

Elasticsearch, a search engine based on Lucene, supports GET requests with a payload, but I would stick to the standards.

Other options are:

Send the JSON as a query parameter

If you go for this approach, the parameter value must be URL encoded. For the JSON you posted in your question, it would be http://example.org/api?query=%5B%7B%22operator%22%3A%22AND%22%2C%22queries%22%3A%5B%7B%22value%22%3A10%2C%22comparisonOperator%22%3A%22%3E%3D%22%2C%22characteristicId%22%3A391%7D%2C%7B%22value%22%3A50%2C%22comparisonOperator%22%3A%22%3C%3D%22%2C%22characteristicId%22%3A391%7D%5D%2C%22characteristicId%22%3A391%7D%2C%7B%22value%22%3Atrue%2C%22comparisonOperator%22%3A%22%3D%22%2C%22characteristicId%22%3A383%7D%5D

Use POST to perform the search

You can assume that the search is a resource and use POST to send the JSON in the request payload to the server.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
1

Q:Is it okay to include such kind of JSON into GET request body ? If no, what is the best way to solve this issue ?

While GET requests MAY technically have body, I wouldn't recommend to rely on this. This looks to me to go against the nature of HTTP protocol.

I think you should POST requests to some /xxx/query resource to perform complex queries.

Community
  • 1
  • 1
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
1

It is fine to have body in GET request. All HTTP clients should have no problems making such requests. Note that operation you perform should be idempotent and free of side effects. In other words, it should be query, not operation that modifies server state.

See https://spring.io/understanding/REST#get for basic rules for RESTfull get.

Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same.

I would not send encoded JSON in query parameter. It will be completely unreadable (because of encoding), therefore useless. It may also be unsafe and reveal too much information, because typically request body is available in web server logs.

Finally, for some request you may hit limit of the URL size of (details depend on client library and server implementation, but limits for URL size tend to be much shorter than limits for body size.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
0

Q:Is it okay to include such kind of JSON into GET request body ? If no, what is the best way to solve this issue ?

You need to consider two issues:

  • GET URL length may exceed the max length. How long it can be it depends on implementation. See

  • You need to URL encode the JSON in query string

Other than that you should be fine.

Community
  • 1
  • 1
isah
  • 5,221
  • 3
  • 26
  • 36
0

You can add your json as an HTML-encoded parameter value. This could make the URL very long, possibly surpassing the maximum URL length limit, therefore this should be considered as an unreliable approach. If your URL will not be used directly by end users, then readability is not really an issue, but if they use it directly, then making the URL as short as possible should be a goal. On the other hand, if the critical features of the page will not work without that JSON, then it could be an incentive to use it as a GET parameter value, possibly encrypting it with base64 or with other means.

In general I pass this kind of long value as a POST parameter, but there are some exceptions as you could see from above.

Technically speaking this should work, but could have issues with long JSONs.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175