0

There is a RESTful API with some resource. I need to GET some resource with parameter which in JSON representation looks like:

{
  "id": int,
  "params":
  [
    {
      "param1": "string",
      "param2": "string"
    },
    {
      "param1": "string",
      "param2": "string"
    }
  ]
}

I have two possible ways to send this object in the query string:

  • id=1&params[0].param1=test&params[0].param2=test&params[1].param1=test&params[1].param2=test
  • id=10000&params[0][param1]=test&params[0][param2]=test&params[1][param1]=test&params[1][param2]=test

The problem is that params array can have a lot of items and the query string can be very long, over 2,000 characters.

To send params in the request body via GET is bad idea.

How I can send such params in a proper RESTful way? Can I use other HTTP method? Or just change the query length on the server?

Oleg Kyrylchuk
  • 1,129
  • 1
  • 10
  • 21

2 Answers2

2

Use a POST method to get some data because params are too long for a GET method ISN'T a bad idea.

You can add the search options in body of request in JSON like

{
  "id": int,
  "params":
  [
    {
      "param1": "string",
      "param2": "string"
    },
    {
      "param1": "string",
      "param2": "string"
    }
  ]
}
Vincent Menant
  • 432
  • 4
  • 10
  • I know that I can send params in body. But adding them to GET verb is a bad idea https://stackoverflow.com/questions/978061/http-get-with-request-body. POST is for creating resources, so I'm wondering if it is RESTful to use it for getting infromation. – Oleg Kyrylchuk Nov 03 '17 at 09:12
  • 1
    POST is MAINLY (and not STRICTLY) for creating resources. If you want absolutely use GET method, headers can be a solution but not more proper than POST solution. – Vincent Menant Nov 03 '17 at 09:41
  • Could you please send some link which confirms your statement? I've read [SO discussion](https://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get), [HTTP specification](https://tools.ietf.org/html/rfc2616) and [W3 article](https://www.w3.org/2001/tag/doc/whenToUseGet.html). You know, technically I probably use the POST. I only want to find confirmation that it is OK, but can't find such info. That's why I asked it here :) – Oleg Kyrylchuk Nov 03 '17 at 10:58
1

If you want an idempotent request URI (i.e. response is always the same), then use GET, else POST.

For more details you can find here for answers:- Why should I POST data rather than GET?

NeeruKSingh
  • 1,243
  • 3
  • 16
  • 24