Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET vs empty POST

Tags:

html

ajax

post

get

For my job, I was looking through a few javascript files, and found a few AJAX calls that used POST, but didn't send any data. It seems they used to, but the backend was updated, and the data wasn't needed, and the previous developers left them as POSTs (or they just copied & pasted the $.ajax calls from other files, and removed the data values).

I changed these empty POST requests to GET requests. I'm assuming it's more efficient to use a GET instead of an empty POST. I've looked, and haven't found anything useful.

So, is it more efficient to use a GET instead of a POST with no data sent?

like image 395
Rocket Hazmat Avatar asked Dec 04 '22 09:12

Rocket Hazmat


2 Answers

I would argue that GET vs. POST isn't an efficiency question so much as a semantic question. What is the intent of the POST requests? If the intent is to alter the state of the system in some way, then I would recommend leaving them as POSTs. If the intent is simply to retrieve some data from the system, then I would change them over to GETs.

The issue of data parameters doesn't really come into play as both GET and POST requests can accept parameters. (GET on the query string and POST via post data)

Outside of theoretical concerns, there are real reasons to use GET or POST. For instance, GET requests can be cached by web servers, proxy servers and clients, whereas POST requests are never cached AFAIK. I'm sure there are other differences, but adhering to the semantic nature of the requests should take care of them for you.

like image 182
John Bledsoe Avatar answered Dec 15 '22 20:12

John Bledsoe


Rather than performance reasons it is the difference of the meaning of the two verbs. GET is supposed not to change the requested resource, whereas POST might do.

like image 43
Bernhard Avatar answered Dec 15 '22 18:12

Bernhard