What HTTP method is the same as the get method but retrieves only the header information and not the document body?


The set of common methods for HTTP/1.1 is defined below and this set can be expanded based on requirements. These method names are case sensitive and they must be used in uppercase.

S.N. Method and Description
1 GET

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.

2 HEAD

Same as GET, but transfers the status line and header section only.

3 POST

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

4 PUT

Replaces all current representations of the target resource with the uploaded content.

5 DELETE

Removes all current representations of the target resource given by a URI.

6 CONNECT

Establishes a tunnel to the server identified by a given URI.

7 OPTIONS

Describes the communication options for the target resource.

8 TRACE

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

GET Method

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 document retrieval. The following example makes use of GET method to fetch hello.htm:

GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive

The server response against the above GET request will be as follows:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Vary: Authorization,Accept Accept-Ranges: bytes Content-Length: 88 Content-Type: text/html Connection: Closed <html> <body> <h1>Hello, World!</h1> </body> </html>

HEAD Method

The HEAD method is functionally similar to GET, except that the server replies with a response line and headers, but no entity-body. The following example makes use of HEAD method to fetch header information about hello.htm:

HEAD /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive

The server response against the above HEAD request will be as follows:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Vary: Authorization,Accept Accept-Ranges: bytes Content-Length: 88 Content-Type: text/html Connection: Closed

You can notice that here server the does not send any data after header.

POST Method

The POST method is used when you want to send some data to the server, for example, file update, form data, etc. The following example makes use of POST method to send a form data to the server, which will be processed by a process.cgi and finally a response will be returned:

POST /cgi-bin/process.cgi HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Content-Type: text/xml; charset=utf-8 Content-Length: 88 Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://clearforest.com/">string</string>

The server side script process.cgi processes the passed data and sends the following response:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Vary: Authorization,Accept Accept-Ranges: bytes Content-Length: 88 Content-Type: text/html Connection: Closed <html> <body> <h1>Request Processed Successfully</h1> </body> </html>

PUT Method

The PUT method is used to request the server to store the included entity-body at a location specified by the given URL. The following example requests the server to save the given entity-body in hello.htm at the root of the server:

PUT /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Connection: Keep-Alive Content-type: text/html Content-Length: 182 <html> <body> <h1>Hello, World!</h1> </body> </html>

The server will store the given entity-body in hello.htm file and will send the following response back to the client:

HTTP/1.1 201 Created Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Content-type: text/html Content-length: 30 Connection: Closed <html> <body> <h1>The file was created.</h1> </body> </html>

DELETE Method

The DELETE method is used to request the server to delete a file at a location specified by the given URL. The following example requests the server to delete the given file hello.htm at the root of the server:

DELETE /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Connection: Keep-Alive

The server will delete the mentioned file hello.htm and will send the following response back to the client:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Content-type: text/html Content-length: 30 Connection: Closed <html> <body> <h1>URL deleted.</h1> </body> </html>

CONNECT Method

The CONNECT method is used by the client to establish a network connection to a web server over HTTP. The following example requests a connection with a web server running on the host tutorialspoint.com:

CONNECT www.tutorialspoint.com HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

The connection is established with the server and the following response is sent back to the client:

HTTP/1.1 200 Connection established Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32)

OPTIONS Method

The OPTIONS method is used by the client to find out the HTTP methods and other options supported by a web server. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server. The following example requests a list of methods supported by a web server running on tutorialspoint.com:

OPTIONS * HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

The server will send an information based on the current configuration of the server, for example:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Allow: GET,HEAD,POST,OPTIONS,TRACE Content-Type: httpd/unix-directory

TRACE Method

The TRACE method is used to echo the contents of an HTTP Request back to the requester which can be used for debugging purpose at the time of development. The following example shows the usage of TRACE method:

TRACE / HTTP/1.1 Host: www.tutorialspoint.com User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

The server will send the following message in response to the above request:

HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Connection: close Content-Type: message/http Content-Length: 39 TRACE / HTTP/1.1 Host: www.tutorialspoint.com User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

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. 

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.  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. 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 :
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
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: 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. 

What HTTP method is the same as the get method but retrieves only the header information and not the document body?


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.

What HTTP method is the same as the get method but retrieves only the header information and not the document body?

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. 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.

Further Learning


REST Java Web Services
REST API Automation testing from scratch-(REST Assured java)
RESTFul Services in Java using Jersey By Bryan Hansen

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 becuase it follows project-based learning which is the best way to learn any new technologies.