HTTP Protocol supports many methods to retrieve data from the server or perform any operation on the server, like upload data, delete the file, etc. In total, the HTTP protocol supports the following methods, GET, POST, PUT, DELETE, HEAD, DELETE, OPTIONS, and TRACE, and the HTTP 1.1 reserves technique called CONNECT for future use. GET, and POST is two of the most common HTTP methods you would hear or work on the web. Though both can be used to send and receive data from client to server, there are some crucial differences between the 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 a programming language independent; it doesn't matter whether your client and server are written in Java or client written in HTML, JavaScript, and Server in Java, or client and server both written in .NET, you will use the HTTP protocol.
In this article, we will learn the pros and cons of the 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 is also a programming language independent; it doesn't matter whether your client and server are written in Java or client written in HTML, JavaScript, and Server in Java, or client and server both written in .NET, you will use the HTTP protocol.
In this article, we will learn the pros and cons of the 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.
Btw, if you want to learn more about how different HTTP methods are used to create REST APIs then I highly recommend you to checkout the REST Java Web Services course on Udemy. This is a brilliant course to understand HTTP and REST.
HTTP Method Descriptions
Before we look into the difference between GET and POST methods, let's see what does each of these 8 HTTP methods does. This will set you up to understand the subtle difference between the GET vs. POST later.1. GET
The 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 an HTTP GET request :
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. If you want to learn more about HTTP protocol, I highly recommend HTTP Fundamentals Course on Pluralsight, one of the best materials to learn HTTP in detail.
GET /home.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.java67.blogspot.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
2. 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 looks 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: https://importexport.amazonaws.com
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. If you want to learn more about HTTP protocol, I highly recommend HTTP Fundamentals Course on Pluralsight, one of the best materials to learn HTTP in detail.
3. 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
4. PUT
The PUT method is used to request the server to store the included entity-body at a location specified by the given URL.
5. DELETE
The DELETE method is used to request the server to delete a file at a location specified by the given URL
6. CONNECT
Establish a tunnel to the server identified by a given URI.
7. OPTIONS
Describe the communication options for the target resource.
8. TRACE
Perform a message loop-back test along the path to the target resource.
If you want to learn more about HTTP and building websites, I suggest you join the Build Responsive Real World Websites with HTML5 and CSS3 by Jonas Schmedtmann on Udemy. It's one of the best courses to learn modern web design, HTML5, and CSS3 step-by-step from scratch. Design AND code a huge project.
Difference between GET and POST Method
Now we know what the GET and POST method does and ow let's understand what the difference between them is:1) GET is a safe method (idempotent), where POST is a non-idempotent method. An HTTP method is said to be idempotent if it returns the same result every time. HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are the idempotent method, and we should implement our application to make sure these methods always return the same result.
HTTP method POST is a non-idempotent method, and we should use the 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 the database, we should use the POST method.
Idempotent methods are also known as safe methods, and we don’t care about the repetitive request from the client for security methods.
2) We can only send limited data with the GET method, and it’s sent in the header request URL, whereas we can send a 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 a similar request again, POST is safe because the information is sent in the request body, and we can’t bookmark it. By the way, this would not be enough if security is a concern because HTTP requests can be intercepted en-route. Better to use HTTPS or SSL encryption to make HTTP communication secure.
4) GET is the default HTTP method, whereas we need to specify the method as POST to send a request with the POST method.
5) Hyper-links in a page uses the GET method. This is usually used to download static content like JPEG images, text files, etc.
6) One more difference between GET and POST method is that GET requests are bookmarkable, like Google Search, but you cannot bookmark a POST request.
7) Like the previous difference, the GET request is also cacheable while you cannot cache POST requests.
8) GET sends data as part of URI, while the 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: java67.com
POST requests are sent in the body of the HTTP request:
POST /index.html HTTP/1.1
Host: java67.com
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 the GET and POST?
Even if you don't know all the difference between the GET and POST method, you can follow this simple rule to choose between the GET and POST method in web applications. Use the GET method for reading data from the server and displaying them, like static HTML pages, images, CSS files, and other resources. Use POST for anything which writes data into the server, e.g. inserting or updating data into the database, uploading flies, deleting an entry, etc.One reason you can not use GET for uploading files using HTTP protocol is that there is a limit on how much data you can send using the GET method, subject to maximum URI length. Another fact you should consider while choosing between the GET vs. POST method is security.
GET is NOT SECURE; whatever data you transfer goes as part of URI, and that's why it's visible to the whole world; you can not send any confidential data using this method. On the other hand, POST sends data as part of the HTTP request body, which can be encrypted using SSL and TLS.
This is the reason all confidential data from client to server transferred using the POST method, like username and password when you log in to internet banking or any online portal.
Similarly, when you book a ticket online, when you make a credit card payment, when you do fund transfer, all data from your browser to the server goes in POST request. If you have written any HTML form, then you know that for registration, login, etc. we use a method as the 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 essential to protect double form submission in your web application. You can prevent this at the client-side using JavaScript or Server-side utilizing some kind of unique tokens.
That's all about the difference between GET and POST methods on HTTP protocol. You should use GET for all static page retrieval operations, which don't contain any secure transaction, while POST should be your default method for sending and receiving data from Server.
Why you should prefer the POST vs. GET method? Because POST can transfer data securely to the server, it can transfer extensive data and should be used to send data to the server.
Other Java REST Web Service tutorials you may like
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are a web developer or want to become a web developer then I suggest you join Build Responsive Real World Websites with HTML5 and CSS3 course on Udemy. This is my favorite course because it follows project-based learning which is the best way to learn any new technologies.
Other Java REST Web Service tutorials you may like
- Top 10 REST Web Service Interview Questions (answer)
- Spring HelloWorld Example using Dependency Injection (tutorial)
- 7 Best Courses to learn Spring Framework (best courses)
- Top 5 Books to learn RESTful APIs and Web Services (books)
- How to create a REST client using the Spring framework in Java? (tutorial)
- How to create a JDBC connection pool using Spring? (tutorial)
- Top 5 courses to learn GraphQL for Beginners (courses)
- The difference between REST and SOAP Web Services? (answer)
- Top 5 Courses to learn RESTFul Web Services in Java? (courses)
- The difference between Idempotent and safe methods in HTTP? (answer)
- How to convert a JSON array to a String array in Java? (tutorial)
- 3 ways to parse JSON in Java? (example)
- 10 Free Courses to learn Spring Boot (Free Courses)
- My Favorite Courses to learn Software Architecture (courses)
- Top 10 Courses to learn Microservices for Java developers (courses)
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are a web developer or want to become a web developer then I suggest you join Build Responsive Real World Websites with HTML5 and CSS3 course on Udemy. This is my favorite course because it follows project-based learning which is the best way to learn any new technologies.
GET - to get the data from Server
ReplyDeletePOST - to send data to Server
GET sends request parameter in URL while POST send it as part of request body
DeleteIf you consider simple html form you can use any of get or post method.If you have sensitive information like password etc and your form have lot of data then you must use post
DeleteUseful basic information in order to understand HTTP requests functioning
ReplyDeletecan you please show the complete implementation of web services
ReplyDelete