HTTP status codes

Eltac Shixseyidov
3 min readAug 7, 2023

--

HTTP status codes are three-digit numbers that provide information about the outcome of a client’s request to a server when interacting with a website or web service. These status codes are a part of the HTTP protocol and convey the result of the requested operation, helping both the client and server understand the outcome of the communication.

These are a fundamental part of the Hypertext Transfer Protocol (HTTP), the underlying protocol of the World Wide Web. These codes are grouped into five classes, each representing a specific type of response:

Here is a detailed explanation of HTTP status codes:

1xx Informational: These codes indicate that the client’s request is being processed or that further action is needed from the client before the server can fulfill the request. An example is the “100 Continue” status, which informs the client to proceed with the request.

2xx Successful: These codes indicate that the client’s request was successfully received, understood, and accepted by the server. The most common is “200 OK,” which signifies that the request was successful and the server is returning the requested data.

3xx Redirection: These codes indicate that the client must take additional action to complete the request. They often involve redirection to a different URL. For instance, “301 Moved Permanently” informs the client that the requested resource has been permanently moved to a new location.

4xx Client Errors: These codes indicate that there was an issue with the client’s request. They often point to client-side errors like malformed requests or unauthorized access. “400 Bad Request” is a common example, indicating that the server couldn’t understand the request.

5xx Server Errors: These codes indicate that the server encountered an error while trying to fulfill the client’s request. These errors are typically caused by server misconfigurations or issues. “500 Internal Server Error” is a well-known code in this category.

Now, let’s provide more detailed explanations and Java examples for a few of these codes:

  • 200 OK: This status code is returned when the server successfully processes the client’s request. It indicates that the requested operation was completed without any issues. This is commonly used for successful GET and POST requests.
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpStatusExample {
public static void main(String[] args) throws Exception {
URL url = new URL("http://example.com/api/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {
// Request was successful, process the response
}
}
}
  • 400 Bad Request: This code indicates that the client’s request is malformed or contains invalid parameters. It’s often used when the server cannot understand the request due to syntax errors or missing information.
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpStatusExample {
public static void main(String[] args) throws Exception {
URL url = new URL("http://example.com/api/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
// Client's request was incorrect, handle the error
}
}
}

But nowadays, the Apache HttpClient library provides a more user-friendly and feature-rich way to interact with HTTP requests and responses. Make sure you have added the Apache HttpClient library to your project before using these examples.

<-- Maven ->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version> <!-- Use the latest version -->
</dependency>
// Gradle
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' // Use the latest version
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

public class HttpStatusExample {
public static void main(String[] args) throws Exception {
HttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://example.com/api/resource");
HttpResponse response = httpClient.execute(request);

int statusCode = response.getStatusLine().getStatusCode();

switch (statusCode) {
case 200:
// Handle success
break;
case 400:
// Handle bad request
break;
case 404:
// Handle not found
break;
case 500:
// Handle internal server error
break;
default:
// Handle other status codes
break;
}
}
}

Using the Apache HttpClient library simplifies working with HTTP status codes and provides a more flexible and powerful approach to making HTTP requests and handling responses.

Understanding these HTTP status codes aids in effective communication between clients and servers and helps manage errors and responses in web applications.

--

--

Eltac Shixseyidov
Eltac Shixseyidov

No responses yet