Search code examples
resturlgetrestful-architecturerestful-url

How to pass a very long array of objects within a query string?


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?


Solution

  • 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"
        }
      ]
    }