DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Writing Good API Status Codes

Writing Good API Status Codes

HTTP status code ranges describe the most common errors you'll see working with Web API integration. The messages on these errors should be easy to understand.

Ross Garrett user avatar by
Ross Garrett
·
Jun. 28, 17 · Opinion
Like (16)
Save
Tweet
Share
9.78K Views

Join the DZone community and get the full member experience.

Join For Free

Error Codes may be the last thing you see from an API - they certainly shouldn’t be the last thing you think about!

Image title

For HTTP, status code ranges can be easily summarized, but as you can see the general “buckets” require a lot more detail for them to be useful.

1xx: Hold Please

2xx: Great Success

3xx: Go Away!

4xx: You Screwed Up

5xx: We Screwed Up

Even 2xx codes can be misleading, often reporting success when something has in fact gone wrong. Sometimes the error is reported within the response payload - and sometimes not at all. So we’re on a mission to help API providers take their status handling more seriously, and ensure developers get the information they need for their applications.

Understanding Status Codes

Status codes are extremely useful. They are the first opportunity - often synchronously after a request - where a server can inform the client that something went wrong. So let’s look more closely at the HTTP Status Code ranges, and some of the common responses users should expect to deal with, since these dominate Web API integration, and you’ll even find vendor-specific codes in this range:

1xx: Hold Please

The 1xx range has two general functions. The first is for information related to state of the connection - such as 101 Switching Protocols. 1xx also clarifies the state of the initial request. 100 Continue, for instance, notes that a server has received request headers from a client, and that the server is awaiting the request body. 

2xx: Great Success

The 2xx range is reserved for success responses. 200 OK means a request was successful, 201 Created confirms that a request has been fulfilled and a new resource has been created for the client, and 202 Accepted means that the request has been accepted.

3xx: Go Away!

The 3xx range relates to status of the resource or endpoint and indicates the client must take additional action to complete the request. For example, 301 Moved Permanently redirects the  client to a different URI for that request.

4xx: You Screwed Up

The 4xx range includes the ever popular and much parodied 404 Not Found. However, there are other status codes useful for APIs in this range too - such as 429 Too many Requests, used to notify a client that requests have reached a rate limited and are being rejected.

Image title

5xx: We Screwed Up

The 5xx range is reserved for status codes related to the server. Unlike 4xx, where the error is the client’s responsibility, 5xx reports when the server has failed for some reason. Unfortunately these aren’t very specific and detail oriented - reporting general failures such as 503 Service Unavailable.

What Makes a Good Status Code

Quality status codes not only communicate what happened, but why it happened. Good status codes must pass three basic criteria in order to truly be helpful:

  1. An HTTP Status Code, to show the source and category of the problem.
  2. A service specific ID code for more detailed status or error description.
  3. A human readable message that summarizes the context, cause, and any solution.

Include Standardized Status Codes

By using a default status, you are providing a good starting point for diagnosis. The 4xx or 5xx range allows the client to know the source of the error. 

Provide Detail

First and foremost, an error code must provide detail. 400 Bad Request means nothing. Passing this information in the body of the response allows service specific errors to be categorized with far greater detail.

Human Context

While machine-readable status codes are key, it’s also important to provide human-readable context. With this context not only do you get the status code, you also get useful, actionable information.

Conclusion 

Best practice is important, yet status codes are often subjective. How you reference links, what error code you generate, and how to display those codes is subject to change from one service provider to another. However, there has been headway to standardize these approaches; the IETF recently published RFC 7807, which outlines how to use a JSON object as a way to model problem details within HTTP response. The idea is that by providing more specific machine-readable messages with an error response, the API clients can react to errors more effectively.

In general, the goal with status responses is to create a source of information to not only inform the user of a problem, but of the solution to that problem as well. This is fundamental to improving the developer experience and delivering a usable, functional API service.

API

Published at DZone with permission of Ross Garrett, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Specification by Example Is Not a Test Framework
  • Using GPT-3 in Our Applications
  • Create a REST API in C# Using ChatGPT
  • Public Key and Private Key Pairs: Know the Technical Difference

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: