What's new

Closed Difference between post and get request in http protocol

Status
Not open for further replies.

imfitp424

Addict
Joined
Jan 11, 2017
Posts
67
Reaction
140
Points
105
Difference between POST and GET Request in HTTP Protocol

HTTP Protocol supports many method to retrieve data from server or perform any operation on server e.g. upload data, delete file etc. In total, HTTP protocol supports following methods e.g. GET, POST, PUT, DELETE, HEAD, DELETE, OPTIONS and TRACE and HTTP 1.1 reserves method called CONNECT for future use. GET and POST are two of the most common HTTP methods you would heard or work in web. Though both can be used to send and receive data from client to server, there are some important difference between GET and POST in HTTP, which will help you to understand when you should use GET vs POST while writing your client and server application. HTTP is also programming language independent, doesn't matter whether your client and server is written in Java, or client written in HTML, JavaScript and Server in Java, or client and server both written in .NET, you will use HTTP protocol. In this article, we will learn pros and cons of GET and POST method to choose, which method you should use in HTML forms, considering facts like security, speed and amount of data to transfer.


HTTP Method Descriptions
Before we look into difference between GET and POST method, let's see what does each of these 8 HTTP method does. This will set you up to understand subtle difference between GET vs POST later.


GET
First HTTP method we will see is the GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data. A GET request retrieves data from a web server by specifying parameters in the URL portion of the request. This is the main method used for static document retrieval. here is an example of HTTP GET request :

GET /home.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: phcorner.net
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive


POST
A POST request is used to send data to the server, for example customer information, file upload etc using HTML forms.
here is how a POST request look like

The following is an example POST request to get the status of a job in AWS.

POST / HTTP/1.1
content-type:application/x-www-form-urlencoded;charset=utf-8
host: phcorner.net
content-length:207

Action=GetStatus&SignatureMethod=HmacSHA256&JobId=JOBID&SignatureVersion=2&Version=2010-06-03&Signature=%2FVfkltRBOoSUi1sWxRzN8rw%3D&Timestamp=2011-06-20T22%3A30%3A59.556Z

The first line represents the type of http request.

Lines 2-4 contain the HTTP headers, including the endpoint of the request.

After the HTTP headers, the body of the request contains the list of parameters. Each parameter is separated by an ampersand (&). The Action parameter indicates the action to perform For a list of the actions, see Actions.

HTML5_logo_and_wordmark.svg.png

HEAD
Same as GET, but only transfer the status line and header section. The HEAD method is functionally like GET, except that the server replies with a response line and headers, but no entity-bod

PUT
The PUT method is used to request the server to store the included entity-body at a location specified by the given URL.

DELETE
The DELETE method is used to request the server to delete file at a location specified by the given URL

CONNECT
Establish a tunnel to the server identified by a given URI.

OPTIONS
Describe the communication options for the target resource.

TRACE
Perform a message loop-back test along the path to the target resource.


Difference between GET and POST Method
Now we know what GET and POST method does and ow let's understand what is difference between them :

1) GET is a safe method (idempotent) where POST is non-idempotent method. A HTTP method is said to be idempotent if it returns the same result every time. HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are idempotent method and we should implement our application to make sure these methods always return same result. HTTP method POST is non-idempotent method and we should use post method when implementing something that changes with every request. For example, to access an HTML page or image, we should use GET because it will always return the same object but if we have to save customer information to database, we should use POST method. Idempotent methods are also known as safe methods and we don’t care about the repetitive request from the client for safe methods.

2) We can only send limited data with GET method and it’s sent in the header request URL whereas we can send large amount of data with POST because it’s part of the request body.

3) GET method is not secure because data is exposed in the URL and we can easily bookmark it and send similar request again, POST is secure because data is sent in request body and we can’t bookmark it. By the way this would not be enough if security is concern because HTTP request can be intercepted en-route. Better use HTTPS or SSL encryption to make HTTP communication secure.

4) GET is the default HTTP method whereas we need to specify method as POST to send request with POST method.

5) Hyper-links in a page uses GET method. This is usually used to download static content like JPEG images, text files etc.

6) One more differnce between GET and POST method is that GET request are bookmarkable, e.g. Google Search, but you cannot bookmark a POST request.

7) Like the previous difference, GET request is also cacheable, while you cannot cache POST requests.

8) GET sends data as part of URI while POST method sends data as HTTP content. GET requests are sent as a query string on the URL:

GET index.html?name1=value&name2=value HTTP/1.1
Host: www.phcorner.org

POST requests are sent in the body of the HTTP request:

POST /index.html HTTP/1.1
Host: www.phcorner.org
name1=value&name2=value

9) GET is limited by the maximum URL length supported by the browser and web server, while POST doesn't have such limits.


How to choose between GET and POST method in HTML
Even if you don't know all the difference between GET and POST method, you can follow this simple rule to choose between GET and POST method in web application. Use GET method for reading data from server and displaying them e.g. static HTML pages, images, CSS files and other resources. Use POST for anything which writes data into server e.g. inserting or updating data into database, uploading flies, deleting an entry etc. One reason you can not use GET for uploading files using HTTP protocol is becuase there is a limit on how much data you can send using GET method, subject to maximum URI length. Another fact you should consider while choosing between GET vs POST method is security. GET is NOT SECURE, whatever data you transfer is goes as part of URI and that's why it's visible to whole world, you can not send any confidential data using this method. On the other hand, POST sends data as part of HTTP request body, which can be encrypted using SSL and TLS. This is the reason all confidential data from client to server transffered using POST method e.g. username and password when you login to internet banking, or any online portal. Similarly when you book a ticket online, when you do credit card payment, when you do fund transfer, all data from your browser to server goes in POST request. If you have written any HTML form then you know that for registration, login etc we use method as post in HTML form. Though you must remember one thing that POST is not idempotent, which means it cannot be safely repeatable. That's why it's very important to protect double form submission in your web application. You can prevent this at client side using JavaScript or Server side using some kind of unique tokens.


That's all on difference between GET and POST method on HTTP protocol. You should use GET for all static page retrieval operations which doesn't contain any secure operation, while POST should be your default method for sending and receiving data from Server. Why you should prefer POST vs GET method? because POST can transfer data securely to server, it can transfer large data and should be used to send data to server.

Sana makatulong sa paggawa nyo ng sarili nyong ehi. enjoy! ;)

imfitp424
 

Attachments

Last edited:
Status
Not open for further replies.
Back
Top