secture & code

Resolving doubts about HTTP [Part 2].

Introduction

In the previous post of Resolving doubts about HTTP, We explored the concept of the HTTP protocol and reviewed the most common methods for interacting with different resources. As I mentioned, the conversation with my partner extended quite a bit as we delved into the status codes we wanted to return in the answers of each operation.

On this occasion, I will talk about some of the most important status codes, how to use them correctly and, to make it more entertaining, I will use a very entertaining website.

Most common status codes

What are status codes in HTTP responses?

Status codes indicate whether an HTTP request has been completed successfully. However, it's not always as straightforward as it sounds (we'll see why later!). To begin with, the codes are grouped into five categories, each representing a numerical range and a specific purpose:

  • Informative answers: codes between 100 and 199.
  • Satisfactory answers: codes between 200 and 299.
  • Redirections: codes between 300 and 399.
  • Customer errors: codes between 400 and 499.
  • Server errorscodes between 500 and 599.

Now that we know these categories, let's explore the most common codes in each group.

Informative codes

  • 100 Continue: Indicates that everything is correct and that the customer can continue with his request.
  • 101 Switching ProtocolThe server agrees to switch to the protocol requested by the client.
  • 102 ProcessingSignals that the server is still processing the request, useful for prolonged operations.
  • 103 Early hintsAllows the client to start preloading resources while the server prepares the final response.

Satisfactory codes

Although there are many codes in this category, here are the highlights:

  • 200 OkMeans that the request has been successful, here depending on the verb of the request it means one thing or another:
    • GET: the server has found the data and has returned it in the body of the response.
    • POST: the server has received the body of the request.
  • 201 CreatedThis code indicates that the resource has been successfully created.
  • 202 Accepted: A code indicating that the request has been accepted by the server but is not yet being processed.
  • 204 No contentThis indicates that the request has been successful but the response does not contain data in the body.

Redirections

  • 301 Moved PermanentlyIndicates that the request was made to a resource that is now at another URI. This url is sent in the response so that the request can be redirected to the new one. It is possible that the method is different in the new URI.
  • 307 Temporary RedirectThis response is sent by the server to ask the client to re-request another URI with the same method used for the previous request.
  • 308 Permanent Redirectmeans that the resource is now permanently located at another URI, specified by the HTTP header response. It is very similar to 301 but this time the method must be the same as the previous request.

Customer errors

  • 400 Bad Request: This error code is displayed when the server is not able to interpret the data sent by the client, for example in a POST request where the body has data that is not expected.
  • 401 UnauthorizedThis code indicates that some kind of authentication is required to get the response from the server.
  • 403 ForbiddenIn this case, although it is similar to 401: with this code we indicate that although you are authenticated you are not authorized to perform this operation.
  • 404 Not foundWho has not seen this error at least once in their life? Indicates that the requested resource does not exist.

Server errors

  • 500 Internal Server Error: The server has encountered a problem, an exception that it does not know how to handle.
  • 502 Bad GatewayThis error usually appears when there is an intermediary, usually a proxy, between the client and the server. It indicates that the intermediary server received an invalid or unexpected response from the end server it is trying to access.
  • 504 Gateway TimeoutYou get this type of error when the connection between the client and the server has timed out because the server has not been able to respond in time.
status codes

Conclusion

As you can see, there is a wide variety of status codes for HTTP responses. In this post we have covered the most common ones, but there are many more. If you want to continue learning in a fun way, check out this page of adorable kittens y puppies. And, as always, the best place to dig deeper is the official documentation from HTTP. 🐾

Full-Stack

Picture of David Pérez López

David Perez Lopez

I like projects to go well
Picture of David Pérez López

David Perez Lopez

I like projects to go well

We are HIRING!

What Can We Do