Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP GET and POST semantics and limitations

Earlier this week, I had to do something which feels like a semantics violation. Let me explain.

I was making a simple AJAX client application, which was to make a request to a service with a given number of parameters. Since the whole app is basically read-only, I thought that using HTTP GET was the way to go. Some of the parameters that I had to pass were simple (such as the sort order, or page number).

However, one of the required parameters could be of variable length, and this made me worry. Since I was encoding all of the parameters in the querystring of the GET request, it seemed to me that this placed an unnecessary upper limit of (roughly) 2000 characters for the request URL. And regardless, I didn't like seeing 500-character-long request URLs.

So, since a POST request doesn't have a limitation like that, I decided to switch. But this doesn't feel right. I am under the impression that a POST denotes modification of data - but I'm using it for a simple read-only request.

Is there a better way to do this? To perform a GET, with many parameters? I've heard of one method - where you perform a preliminary POST of the parameters themselves, and then perform a GET. But, this technique leaves much to be desired.

But looking past this specific case, what are the real semantics and limitations of HTTP request methods? And why does GET not support any kind of parameter payload? Using the querystring in the URL almost feels like a hack to me.

like image 748
voithos Avatar asked Jun 10 '12 05:06

voithos


People also ask

What is the limitation of GET method?

Disadvantages of GETGET can't be used to send word documents or images. GET requests can be used only to retrieve data. The GET method cannot be used for passing sensitive information like usernames and passwords. The length of the URL is limited. If you use GET method, the browser appends the data to the URL.

What is the difference between HTTP GET and POST?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but Main difference between POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from client to ...

What are the 4 types of HTTP request methods?

The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE.

What is difference between HTTP methods GET POST put and delete?

The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server. The PUT method replaces all current representations of the target resource with the request payload. The DELETE method deletes the specified resource.


1 Answers

A few points on this issue:

  • The HTTP spec (RFC 2616) doesn't forbit GET requests to have parameters, so it's not a matter of the semantics of HTTP GET itself. However, many HTTP stacks (for clients, services, or proxies) forbid bodies in HTTP requests, the fact that you can't use them is mostly an implementation detail (quite prevalent) than a semantic issue with the HTTP GET requests
  • Similarly, the limitation of the URI (or query string) length isn't specified on the RFC either. It's mostly a security mitigation implemented by several HTTP server stacks to prevent a bad client from consuming server resources (for example, in IIS/ASP.NET the default limit is 2k but you can increase it via some elements in web.config). Again, it's not a semantic but a practical issue.
  • POST requests do indicate data modification if you're following the REST philosophy, but there are many examples of HTTP POST requests used for read-only operations. SOAP uses POST in all of its requests, regardless of whether the operation it is calling is a "safe" or a "modifying" one. So you can use POST for those operations as well. However, by deviating from the REST (and the "canonical" HTTP) usage, you'll lose some of the features of the protocol, such as caching which can be applied for GET requests, but not for POST.
  • Your example of using two requests (POST with parameters + GET to "get" the results) seems overkill. As I mentioned, POST requests don't necessarily mean modifying resources, so you don't have to create a new "protocol" (POST+GET) to access your operation when one request is enough.
like image 156
carlosfigueira Avatar answered Nov 19 '22 08:11

carlosfigueira