2

I am facing a problem within a PHP Restful API ...

I am trying to GET some products informations (weight, process, price ...) by calling this API.

The problem is that the provided URI can contain a lot of characters because we are trying to make as less API calls as possible.

As you can understand, if we send a too long URI using HTTP GET method, I will surely finish with a 414 or 500 HTTP ERROR.

So how can we :

  1. Send an HTTP GET request to get informations having a long URI
  2. Stay RESTFUL (without using POST as if I refer to the Restful documentation -> That is the major issue)

Here is an example of a request :

http://<myWebsite>/rprocess/api/process/{"data":["FLY.DELeco.LOT100.DIM10x10.FACrv.SUPdemma.GRA350.PELbrv.VERss","FLY.DELnor.LOT500.DIM20x30.FACr.SUPcoubr.GRA300.PELbrv.VERss"]}
  • As you can see, we send a JSON encoded string in the URI.
  • This example works because we only try to get [process] informations for two products (we want to make a call with a lot more)

I have already searched for an answer on this problem but it looks like we can not stay restful in that case.

Here are the answers I have found:

  1. Use POST and add set headers to X-HTTP-Method-Override: GET
  2. Increase the url limit size on the server (not recommended)
  3. Split the GET requests in order not to reach the url length limit (that's not a good idea because the API can handle way more product at a time as we can actually send it ...)

I hope those informations can help you answer my question :)

Thank you for your help

Romain Masclef
  • 121
  • 1
  • 8

1 Answers1

0

Maybe you could try looking up your data by id if you have a normalised database table to help shorten your request

or you could try POSTing the query to the server, storing the details of the request and then perform a GET request using the ID of the previously stored request

arcanine
  • 1,933
  • 1
  • 15
  • 22
  • Thank you for your answer ! I don't think that the problem is there ... Try to imagine sending a json with '12345' instead of our "FLY...", at some point we will reach the limit of URI length even if the ID is smaller. – Romain Masclef Jun 25 '15 at 12:39
  • Is it worth considering the use of pagination on the client? That way you can retrieve more manageable chunks – arcanine Jun 25 '15 at 21:22
  • Pagination will result in this answer : Split the GET requests in order not to reach the url length limit (that's not a good idea because the API can handle way more product at a time as we can actually send it ...) – Romain Masclef Jun 29 '15 at 06:43
  • EDIT : Pagination will result in this answer : `Split the GET requests in order not to reach the url length limit (that's not a good idea because the API can handle way more product at a time as we can actually send it ...)` This request is, in my case, only used in a script so we don't need any pagination ;) Maybe I explained the problem in a wrong way ... Try to imagine that you want to search something with many filters. So much filters that the generated URI is too long. `mySite/products/{filter1:, filter2:, ...}` – Romain Masclef Jun 29 '15 at 07:26
  • I'm facing exactly the same problem as you are. One of my developers suggested encrypting the parameters (so it can be shorter) when sending the request, and decrypting when need to read it on server side. Haven't tried. Think that's good practice? – Thomas Cheng Sep 06 '16 at 16:37
  • http://stackoverflow.com/questions/14202257/design-restful-query-api-with-a-long-list-of-query-parameters – arcanine Sep 14 '16 at 14:10