Can we send payload in HTTP GET request ?
Short answer? yes. But there is a high chance that you never did and it’s high time we discuss why.
How did I discover it?
In my org., an internal tool that I’m currently working on is using Elasticsearch, which is popular with ELK stack. It instantly caught my interest and I wanted to play around with it and also to understand it’s underlying architecture and engineering sometime.
So I did set it up to check out. In order to perform a search API call, a GET request with payload is to be sent according to documentation.
Well, it didn’t catch my attention the very first time but I recognized it next time as GET with payload which I never saw before. I started researching about it which eventually led me to share info in this thread.
The clarification we need is given by Elasticsearch team in here and I’m writing few points from there below.
According to RFC 7231, the RFC that deals with HTTP semantics and content, does not define what should happen to a GET
request with a body! As a result, some HTTP servers allow it, and some—especially caching proxies—don’t.
** A Request for Comments (RFC) is like a document by the Internet Engineering Task Force (IETF) which contains spec.s and notes about the internet and computer networking **
The authors of Elasticsearch prefer using GET
for a search request because they feel that it describes the action—retrieving information—better than the POST
verb. However, because GET
with a request body is not universally supported, the search
API also accepts POST
requests.
Elasticsearch did justify why they used GET with payload for their search API.
Advantages & Disadvantages
It does have it’s own advantages like no restricted length and encoding json will be easy.
But it also comes with disadvantages like payload not being read by proxies, thus breaking cache and hit performance issues. Also GET is no more idempotent as it’s changing response based on payload.
Should we use ?
There could be bad consequences that are to be faced if we use like
Java Spring not reading GET payload, HttpMessageNotReadableException
And same with Javascript fetch API. Payload may even get ignored.
So even though GET offers to send payload with request, it’s better not to send any request payload which can cause any bad consequences and follow semantics or it is okay to use POST.