PHP - restful API - HTTP GET request with long URI

916 Views Asked by At

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

1

There are 1 best solutions below

6
On

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