Java Get PostHypertext 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 ProgramThe following program demonstrates how one can make the GET request to a server. FileName: JavaGETExample.java Output: JSON String Result is: { "data": { "id": 3, "name": "true red", "year": 2002, "color": "#BF1932", "pantone_value": "19-1664" }, "support": { "url": "https://reqres.in/#support-heading", "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: When we sent the above request, the server sends back the following response to the user. 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 ProgramThe following program demonstrates how one can make the POST request to a server. FileName: JavaPOSTExample.java Output: { "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: The following snapshot shows the response from the server. The response code is 201, which is the same as it is shown in the output of the program. Differences between GET and POSTThe following table shows the major differences between the GET and POST.
Next TopicFork Join in Java
|