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?

2 Answers 2

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"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know that I can send params in body. But adding them to GET verb is a bad idea 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.
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.
Could you please send some link which confirms your statement? I've read SO discussion, HTTP specification and W3 article. 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 :)
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.