Background

This seems to be a very popular question for any web development interview to judge the basic knowledge of the candidate. To my surprise, when I was having my face to face interviews with D E Shaw, Hyderabad, I faced three different interviewers that day and this question was asked by all three of them. I was not very comfortable in answering this question and when I came back, Google search around the same did not help me get one convincing answer. So here I am to write it for you!!

Answer

GET and POST are simply two ways of communication between our web browser and a web server. Whenever we launch a browser and type in some url, browser sends and HTTP GET request to the server to get the page contents. So, GET is pretty much clear, right?

Now, An HTML page may have some form where we need to enter some information and press a submit button. Like for a google search we type in what we need in a text box and just hit an enter. Here developer needs to decide which method to use - GET (which is default if developer doesn't sepecify anything) or POST.

  
< form method="GET/POST" action="somescript.asp" >

GET - It means that form data to be submitted will be encoded in the URL by browser and sent to the server. e.g. Google search for CodeBoat will submit data as

  
http://www.google.co.in/search?hl=en&q=codeboat&btnG=Search&meta=

You can just view source of Google.com home page you will find that form tag doesn't have a method specified which means it is using default method GET.
  
<form action="/search" name=f >

POST - It means that form data needs to be submitted by browser as message body and not in URL.

So, Thumb Rule is - GET is meant for retrieving the data and POST is meant for sending data that updates database on server.
Important Points to Remember
  • GET method has a limitation because URLs in may old browsers can not be more than 255 characters.
  • GET can not be used when secure data needs to be sent to server. In case where a username and password are being sent to server for user validation.
  • POST can handle as much data and is meant for sending data securely to the server.
If you have any query/question or feedback. Feel free to send them over to codeboat@gmail.com

0 comments