Junior — Middle
Which HTTP method is preferred for sending user credentials such as login and password?
sobes.tech AI
Answer from AI
To send user credentials such as login and password, it is preferable to use the HTTP method POST.
Reasons:
- Data is transmitted in the request body, not in the URL, which enhances security (URLs can be logged and cached).
- POST allows sending larger amounts of data.
It is important to use HTTPS to encrypt the data during transmission.
Example of sending data using POST:
// Using HttpURLConnection in Android
URL url = new URL("https://example.com/login");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
String postData = "username=user&password=pass";
try(OutputStream os = conn.getOutputStream()) {
byte[] input = postData.getBytes("utf-8");
os.write(input, 0, input.length);
}