In this post, we will learn the difference between GET and POST Methods in detail. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Feature | GET Method | POST Method |
---|---|---|
Request Type | Used to request data from the server. | Used to submit data to the server. |
Data in URL | Data is appended to the URL in the form of query parameters. | Data is sent in the request body and not visible in the URL. |
Data Size Limit | Limited by the maximum URL length (usually around 2048 characters). | Not limited by URL length; can handle large data sets. |
Caching | Responses can be cached by the browser or proxy servers. | Responses are typically not cached by browsers or proxy servers. |
Idempotent | GET requests are idempotent, meaning multiple requests with the same parameters yield the same result. | POST requests are not idempotent, multiple requests may lead to different results. |
Data Security | Less secure as data is exposed in the URL and can be logged in browser history. | More secure as data is not exposed in the URL and is transmitted in the request body. |
Bookmarking | Can be bookmarked and shared as the data is part of the URL. | Should not be bookmarked or shared as it may contain sensitive data. |
Data Type | Can only send data in ASCII format. | Can send various data types, including binary data and multipart forms. |
Usage | Used for retrieving data from the server, like fetching web pages, API data, etc. | Used for submitting data to the server, like form submissions, file uploads, etc. |
GET Method Example:
In this example, we will use the GET method to send a request to a server and receive a response. The GET method is used to request data from the server, and the data is sent as part of the URL.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetExample {
public static void main(String[] args) {
try {
// Specify the URL to send the GET request
String url = "https://api.example.com/data?id=123";
// Create a URL object
URL obj = new URL(url);
// Open a connection using HttpURLConnection
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Set the request method to GET
con.setRequestMethod("GET");
// Get the response code
int responseCode = con.getResponseCode();
// Print the response code
System.out.println("Response Code: " + responseCode);
// Read the response from the server
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Print the response
System.out.println("Response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
POST Method Example:
In this example, we will use the POST method to send data to a server. The POST method is used to submit data to be processed to a specified resource.
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostExample {
public static void main(String[] args) {
try {
// Specify the URL to send the POST request
String url = "https://api.example.com/data";
// Create a URL object
URL obj = new URL(url);
// Open a connection using HttpURLConnection
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Set the request method to POST
con.setRequestMethod("POST");
// Set the request headers
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Set the data to be sent in the request body
String data = "id=123&name=John";
// Enable the output stream for writing data to the server
con.setDoOutput(true);
// Get the output stream
DataOutputStream out = new DataOutputStream(con.getOutputStream());
// Write the data to the output stream
out.writeBytes(data);
// Close the output stream
out.close();
// Get the response code
int responseCode = con.getResponseCode();
// Print the response code
System.out.println("Response Code: " + responseCode);
// Read the response from the server
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Print the response
System.out.println("Response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
The GET method sends data in the URL, while the POST method sends data in the request body. The appropriate method to use depends on the specific use case and the type of data being transmitted. GET is typically used for retrieving data, while POST is used for submitting data.
Comments
Post a Comment