Javatpoint Logo
Javatpoint Logo

Java Get Post

Hypertext Transfer Protocol (HTTP) supports many methods to do any task on the server or to receive any data from a server. The Java Get and Post methods are two prominent methods of HTTP for sending and receiving data from a server. Even though both methods can be used to send or retrieve data from the server, there are some major differences between these two methods.

What is GET method?

GET method is used to appends form data to the URL in name or value pair. If we use GET, the length of URL will remain limited. It helps users to submit the bookmark the result. It is better for the data which does not require any security or having images or word documents.

What is POST method?

POST is a method that is supported by HTTP and depicts that a web server accepts the data included in the body of the message. It is often used by World Wide Web to send user generated data to the web server or when you upload file.

Let's create Java programs to see how to use GET and POST request.

Java Get Request Program

The following program demonstrates how one can make the GET request to a server.



JSON String Result is: 
    "data": {
        "id": 3,
        "name": "true red",
        "year": 2002,
        "color": "#BF1932",
        "pantone_value": "19-1664"
    "support": {
        "url": "",
        "text": "To keep ReqRes free, contributions towards server costs are appreciated!"

Explanation: Let's understand the code line by line.

In the above code, we get the API or Server with the help of its URL.

The HTTP GET request to receive its content.

While establishing the connection, we get the response code. The response code determines whether the connection has been established or not.

After the successful connection establishment, we open the InputStreamReader and BufferedReader to read whatever the API or server is sending.

Using the while-loop, the data from the server is read and stored in a String variable.

After storing the response, the BufferedReader is closed and the connection is disconnected.

In the end, the stored response is being displayed on the console using the print statement.

To test whether the URL is working or not, one can also take the help of the POSTMAN tool. The following snapshot of the POSTMAN tool shows the same.

Sending Data in the Header:

Java Get Post

When we sent the above request, the server sends back the following response to the user.

Java Get Post

Observe the URL, we have sent information in the URL i.e. id = 3, that is visible. It shows why secure data cannot be sent using the GET request.

Java Post Request Program

The following program demonstrates how one can make the POST request to a server.



"userId": 199,
 "id": 101,
 "title": "About JavaTpoint",
 "body": "JavaTpoint is a good site to learn Java. One must visit the site."
The POST Request Response Code:  201
The POST Request Response Message: Created
Response from the server is:
"userId": 199,
 "id": 101,
 "title": "About JavaTpoint",
 "body": "JavaTpoint is a good site to learn Java. One must visit the site."

Explanation: In the above program, we have sent a POST request to the sever. We have pushed data through URL.

In the POST request, we are sending a message to the server, that is presented in the URL mentioned in the above program. The OutputStream object helps to write content to the server. After that, the server acknowledges the user about data by response code. Then, reading from the server is done. It is the same as we did in the GET request. Note that message content is sent in the request body. To achieve the same, the following statement is used in the code.

The above statement is not used in the GET request as we are only receiving information from the server.

Let's validate the same using the POSTMAN tool.

The following snapshot shows the input we are sending to the server. Notice, we have selected the Body tab, and the format is JSON for the message content. Also, the message content is not visible in the URL that shows POST is more secure than GET.

In our case, we have posted the following data:

Java Get Post

The following snapshot shows the response from the server.

Java Get Post

The response code is 201, which is the same as it is shown in the output of the program.

Differences between GET and POST

The following table shows the major differences between the GET and POST.

Get Post
In the case of GET request, data is sent in the header. Therefore, a limited amount of data can be sent using the Get request. In the case of POST request, data is sent in the body. Therefore, a large amount of data can be sent using the POST request.
GET fetches the request from the URL. Hence, the data is visible and not secure. Therefore, sensitive information should not be used along with the GET request. In the case of POST method, the data is carried in the body in case of the POST request, the data is not visible and hence more secure. Therefore, sensitive information can be used with the POST request.
One can bookmark the GET request. One cannot bookmark the POST request.
The state of the server does not get changed on the GET request. Hence, the GET request is always idempotent. The state of the server does get changed on the POST request. Hence, the POST request is not idempotent.
GET request is more efficient. POST request is less efficient.
GET request remains in the browser history. POST request does not remain in the browser history.

Next TopicFork Join in Java

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA