Which HTTP Response Code to Use?

I have the HTTP verbs and URL that define the request. And I have the HTTP response code that defines my response.

All the parts of a request must be clear and understandable.

The most used one is 200, which means Ok. I can use it in almost all the cases.

However, there are other important HTTP response codes for different situations.

In this article, I list the most important HTTP response codes I use in my projects.

200

It means the request has succeeded, and the requested information is in the response body.

Examples:

  • Read a single vehicle: Ok, here you are;
  • Search vehicles from a search criteria: Ok, here you are.

201

After creating an entity, this is the code returned. The 201 HTTP response code means Created. Along with the code, comes the body response with the created entity and an HTTP Header Location with the URL where to find the created entity.

Example:

  • Create a vehicle: Created, here is the created vehicle and the URL where to find it.

204

When I have nothing to respond, just say Ok and it returns no content. The 204 has no response body.

Example:

  • Update the status of my order: Ok, nothing more to say.

301

The 301 HTTP response code means that the requested information is no more available at the given URL, and you can find it in the returned one.

This HTTP response code comes with a Location HTTP header, as the 201 code, where to find the requested information.

This HTTP response code is commonly used to inform Search Engines about changes in a website.

Examples:

  • When I request a web page which is now in another URL;
  • When I request a website which changed its domain name.

302

The 302 HTTP response code is like the 301, which means that the information is not at the requested URL, it has moved to another, but this is temporary.

As for the previous one, this HTTP response code is commonly used to inform Search Engines about changes in the website.

Example:

  • When I request a web page which is under maintenance, but the content is available elsewhere.

400

When the user tries to make a bad request.

Examples:

  • Sell the given vehicle: the vehicle was already sold;
  • Send the merchandise of the given order: the order wasn’t bought.

401

When the operation is not permitted for this user because he’s not authenticated.

Example:

  • Show me my orders: you must first authenticate yourself.

403

When the operation is not permitted for this user, even if the user is authenticated, he does not have enough permissions.

Example:

  • Show me the orders of customer 123: you have no permission to see this information.

404

When the requested entity was not found on the server.

Examples:

  • Read the information about a single vehicle: the vehicle doesn’t exist;
  • Give me the item of my basket: you don’t have a basket.

405

When the HTTP method is not allowed.

Examples:

  • Create a new vehicle: you can only read the vehicles;
  • Update the given vehicle: it’s a read-only endpoint.

500

When there was an unexpected error.

Examples:

  • The database is down;
  • The server is down;
  • There was a programming error.

501

When the requested operation was not yet implemented.

Examples:

  • Buy the given vehicle: the sales service is not yet implemented;
  • Export the list of vehicles: the export has not yet being implemented.

503

When the service is unavailable.

Examples:

  • When the service is under maintenance;
  • When the service is down due to a problem.

504

When the server was unable to respond within the time range accepted by the Gateway or Reverse Proxy.

  • The response time of the server is 3 minutes, but the Reverse Proxy cancels the request when it exceeds 1 minute;
  • The search takes more time than expected, and the server cancels the request when it exceeds 30 seconds.

Conclusion

The response codes can be grouped into 5 groups, 1xx for informational requests, 2xx for ok, 3xx for moved entities, 4xx for errors made by the user, and 5xx for errors made by the server.

I’ve never used the 1xx codes, so I found it unnecessary to explain them.

CodeMeaningFeatures
200OkHas a body
201CreatedHas the Location HTTP header
204No contentHas no body
301MovedHas the Location HTTP header
302FoundHas the Location HTTP header
400Bad requestIt’s a user error
401UnauthorizedIt’s a user error
403ForbiddenIt’s a user error
404Not foundIt’s a user error
405Method not allowedIt’s a user error
500Internal server errorIt’s a server error
501Not implementedIt’s a server error
503Service unavailableIt’s a server error
504Gateway timeoutIt’s a server error

Never Miss Another Tech Innovation

Concrete insights and actionable resources delivered straight to your inbox to boost your developer career.

My New ebook, Best Practices To Create A Backend With Spring Boot 3, is available now.

Best practices to create a backend with Spring Boot 3

One response to “Which HTTP Response Code to Use?”

  1. […] I describe in detail the most used HTTP response codes in this article. […]

    Like

Leave a comment

Discover more from The Dev World - Sergio Lema

Subscribe now to keep reading and get access to the full archive.

Continue reading