Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest POST VS GET if payload is huge

Tags:

I understand the definition of GET and POST as below.

GET: List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.

POST: Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.

MY API searches for some detail in server with huge request payload with JSON Message in that case Which Verb should i use ?

Also can anyone please let me know the length of the characters that can be passed in query string.

like image 781
Ravi Avatar asked May 20 '15 06:05

Ravi


2 Answers

The main difference between a GET and POST request is that in the former, the entire request is encoded as part of the URL itself, whereas in the latter, parameters are sent after the header. In addition, in GET request, different browsers will impose different limits on how big the URL can be. Most modern browsers will allow at least 200KB, however Internet Explorer seems to limit the URL size to 2KB.

That being said, if you have any suspicion that you will be passing in a large number of parameters which could exceed the limit imposed on GET requests by the receiving web server, you should switch to POST instead.

Here is a site which surveyed the GET behavior of most modern browsers, and it is worth a read.

like image 196
Tim Biegeleisen Avatar answered Oct 19 '22 00:10

Tim Biegeleisen


Late to the party but for anyone searching for a solution, this might help.

I just came up with 2 different strategies to solve this problem. I'll create proof of concept API and test which one suites me better. Here are the solution I'm currently thinking:

1. X-HTTP-Method-Override:

Basically we would tunnel a GET request using POST/PUT method, with added X-HTTP-Method-Override request header, so that server routes the request to GET call. Simple to implement and does work in one trip.

2. Divide and Rule:

Divide requests into two separate requests. Send a POST/PUT request with all payload, to which server will create necessary response and store it in cache/db along with a key/id to access the data. Then server will respond with either "Location" header or the Key/id through which the stored response can be accessed.

Now send GET request with the key/location given by server on previous POST request. A bit complicated to implement and needs two requests, also requires a separate strategy to clean the cached responses.

like image 36
Nripendra Avatar answered Oct 18 '22 23:10

Nripendra