DEV Community

Cover image for HTTP Status Codes: A Guide for Developers
mahdi
mahdi

Posted on

HTTP Status Codes: A Guide for Developers

HTTP status codes are essential communication tools between web servers and clients, providing vital information about the outcome of a client's request. These three-digit numerical codes categorize responses into different groups, each conveying specific information about the status of the request. Understanding these status codes is crucial for developers, as they help diagnose and troubleshoot issues that occur during web interactions.

What Exactly Are HTTP Status Codes?

HTTP status codes are standardized numerical codes served by a web server in response to a client's request made to the server. These codes convey information about the success or failure of the request and are grouped into different classes based on the first digit of the status code.

Informational Responses - 1xx

1xx status codes are informational responses indicating that the client's request has been received and the server is continuing the process. These provisional responses are used to inform the client that the server has received the request and is still processing it. Common 1xx status codes include:

  1. 100 - Continue

    Concept: Indicates that the initial part of the request has been received successfully by the server, allowing the client to proceed with the rest of the request. This code informs the developer that the server is ready for the client to send the remainder of the request.

  2. 101 - Switching Protocols

    Concept: It signifies that the server agrees to the client's request to switch protocols and that the protocol change is happening. Developers should expect subsequent communication to occur using the newly requested protocol.

  3. 102 - Processing

    Concept: This status indicates that the server has received and is processing the request but hasn't completed it yet. It's helpful for developers as it informs them that the server has acknowledged the request and is actively working on it.


Success Responses - 2xx

2xx status codes indicate that the client's request was successfully received, understood, and accepted. These codes signify that the requested action has been successfully received, understood, and accepted. Examples of 2xx status codes include:

  1. 200 - OK

    Concept: Indicates that the request was successful and that the server has fulfilled the request made by the client. This status assures developers that their request was processed correctly.

  2. 201 - Created

    Concept: Confirms that the request has been successfully fulfilled and has resulted in the creation of a new resource as requested by the client. Developers can use this response to confirm the successful creation of resources.

  3. 204 - No Content

    Concept: It indicates that the server has successfully processed the request but does not need to return any content. Developers can utilize this status in scenarios where a response body isn't necessary.


Redirection Messages - 3xx

3xx status codes inform the client that further action needs to be taken to complete the request. These status codes are used for redirection. Some common 3xx status codes are:

  1. 301 - Moved Permanently

    Concept: Tells developers that the requested resource has been permanently moved to a new location. It's crucial for developers to update their links to the new location to avoid broken links.

  2. 302 - Found

    Concept: Informs developers that the requested resource is temporarily located elsewhere. It suggests that the client should continue using the original URL or the new one, depending on the context.

  3. 303 - See Other

    Concept: It indicates to developers that the response to the request can be found at a different URI and should be retrieved using a GET method. This is often used in redirection after a POST request.


Client Error Responses - 4xx

4xx status codes indicate that there was an error on the client's side. These errors often stem from the client’s request and can be related to incorrect syntax or the unavailability of a resource. Examples of 4xx status codes include:

  1. 400 - Bad Request

    Concept: Indicates that the server cannot process the request due to a client error, often due to malformed syntax or invalid parameters in the request. Developers need to review and fix the client's request.

  2. 401 - Unauthorized

    Concept: Denotes that the request lacks valid authentication credentials or the provided credentials are invalid. It signifies that proper authentication details are required to access the requested resource.

  3. 403 - Forbidden

    Concept: Indicates that the server understood the request but refuses to authorize it. Unlike 401, here, the client's credentials are valid, but access to the resource is denied due to insufficient permissions or authentication issues.

  4. 404 - Not Found

    Concept: Indicates that the requested resource is not available on the server. It's essential for developers to handle this response by providing appropriate feedback to users or locating alternative resources.


Server Error Responses - 5xx

5xx status codes indicate that the server failed to fulfill a valid request from the client due to an error on the server's end. These errors generally indicate problems with the server or its configuration. Some common 5xx status codes are:

  1. 500 - Internal Server Error

    Concept: Informs developers that something went wrong on the server's end while processing the request, without specifying the exact cause. It suggests an issue within the server that needs to be addressed.

  2. 503 - Service Unavailable

    Concept: Notifies developers that the server is currently unable to handle the request due to maintenance or overloading. This status is temporary and advises developers to try again later.

  3. 504 - Gateway Timeout

    Concept: This status indicates that the server acting as a gateway or proxy did not receive a timely response from an upstream server. It suggests that developers check the communication between servers or services.

Conclusion

Stepping through the world of HTTP status codes might feel like exploring a new city. Each code, like a street, guides us towards a better understanding of how the digital world communicates. From the informational avenues of 1xx to the bustling success boulevards of 2xx, each provides valuable insights into the complex web infrastructure.

As we journey into the redirection roundabouts of 3xx, we learn the art of navigational flexibility. The erroneous alleyways of 4xx teach us the humbling lessons of resilience and troubleshooting. And inevitably, when we encounter the server-side slip roads of 5xx, it reminds us that even the strongest systems have their rainy days.

Now that we've toured the city of status codes together, I hope you feel more confident navigating its lanes and byways. Remember, each code is more than a number; it's a conversation between server and client, a subtle dance of request and response that keeps our online worlds buzzing.

Let’s exchange ideas, solutions, and maybe even a few laughs about the quirks of server-client communication. You can get in touch with me via email at mahdimamashli13831@gmail.com—let’s keep the digital conversation alive and thriving.

Perhaps you’re more inclined to tweet your thoughts or share your latest developer victory or conundrum. If that’s the case, my Twitter handle is @m_mdy_mLet’s connect and contribute to the tech dialogue in the Twittersphere.

For those who love to build, share, and collaborate, my GitHub repository is a playground for innovation. Visit me at m-mdy-m and let's code our way to new horizons.

I wholeheartedly look forward to our exchanges beyond this article. Every email, tweet, or pull request is a step towards greater knowledge and community. After all, isn't connection what the web is all about?

Until our paths cross again in the matrix of code and pixels, may your requests always find their way, your logic be bug-free, and your servers run as smoothly as a perfect cup of code brew.

Top comments (9)

Collapse
 
vechet profile image
Chuo Vechet

Good sharing.

Collapse
 
anthony_hagi profile image
Anthony Hagidimitriou

Nice post! One thing that I believe you should add/make a note of is 401 Unauthorised requests. This status code is somewhat common and needs to be understood.

Highlighting the difference between a 401 and 403 response is also crucial to understand, as they seem very similar yet are different.

As a quick summary for those who may not know:

401 Unauthorised - you didn’t supply any credentials or your credentials are invalid
403 Forbidden - you did supply credentials and they are correct, but you do not have access to the resource (e.g permissions)

Collapse
 
m__mdy__m profile image
mahdi

Thank you very much, I will definitely add it

Collapse
 
m__mdy__m profile image
mahdi

Yes, http status codes are very useful for developers

Collapse
 
alanclev profile image
Alan

Great post thank you!

Collapse
 
ravavyr profile image
Ravavyr

Something to clarify here is that as a backend dev you can tell the server what to respond with. Some devs for example like to add a 400 status even though the server is fine when their API returns an error message [not the same as an actual error]. I personally hate this type of setup but some devs like it.

So if you're ever in that kinda mood you can just make all requests to your server return a 418 "I'm a Teapot" response.
developer.mozilla.org/en-US/docs/W...

Collapse
 
sreno77 profile image
Scott Reno

Nice guide!

Collapse
 
nchandan profile image
NChandan

You misssed 418😉

Collapse
 
humjerry profile image
Humjerry⚓

Nice one brother.