What are REST API error handling best practices?
Tutorial by Er Satya
All > Tech Blogger | Java | Spring Boot | HTML | CSS | MySQL > Java | Spring Boot
1 like
Please login to like this article.
What is REST API error handling best practices?
- You will need to carefully pick 4xx codes that fit your error conditions. You can include a rest, XML, or plaintext message as the payload that includes a sub-code and a descriptive comment.
- The clients will need to use a software framework that enables them to get to the HTTP-level status code. Usually doable, not always straightforward.
- The clients will have to distinguish between HTTP status codes that indicate a communications error and your own status codes that indicate an application-level issue.
- The HTTP Status code is NOT part of your API
- The HTTP status code will always be 200 if your app received the request and then responded (both success and error cases)
- All of your responses should include "envelope" or "header" information. Typically something like:
envelope_ver: 1.0 status: # use any codes you like. Reserve a code for success. msg: "ok" # A human string that reflects the code. Useful for debugging. data: ... # The data of the response, if any.
- This method can be easier for clients since the status for the response is always in the same place (no sub-codes needed), no limits on the codes, and no need to fetch the HTTP-level status code.
- Be sure to include version numbers so you can later change the semantics of the api if needed.
- Document
5xx
Server Error
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates (RFC 2295 )
507 Insufficient Storage (WebDAV) (RFC 4918 )
509 Bandwidth Limit Exceeded (Apache bw/limited extension)
510 Not Extended (RFC 2774 )
2xx Success
200 OK
201 Created
202 Accepted
203 Non-Authoritative Information (since HTTP/1.1)
204 No Content
205 Reset Content
206 Partial Content
207 Multi-Status (WebDAV)
response
, data
and message
properties. The response I believe contains true
or false
to indicate if the operation was successful (usually for write operations). The data contains the payload (usually for read operations) and the message contains any additional metadata or useful messages (such as error messages when the response
is false
).
More articles from same author:
More articles:
Recent lost & found:
Login for enhanced experience
Create and manage your profile
Refer an author and get bonus Learn more
Publish any lost and found belongings
Connect with the authors & add your review comments
Join us for Free to advertise for your business or Contact-us for more details
Join us for Free to publish your own blogs, articles or tutorials and get your Benefits
Discover your area of interest
Advertisement
Art & entertainment
Astrology & spirituality
Cooking
Culture
Current affairs
Education
Fashion
History
Hotel management
Industry
Medical & fitness
Motivational
Politics
Real life stories
Sports
Story & poetry
Technology
Top in search
Tourism
More recent categories
Nutella Chocolate For Sale In Europe- Where To Buy Nutella Chocolate In Europe(Public)
By: GREENSPZOO
How To Buy Nutella in Dubai- Nutella For Sale in Dubai(Public)
By: GREENSPZOO
Wholesale Nutella and Confectionery Products in Italy(Public)
By: GREENSPZOO
Top-Grade Nutella Chocolate for Sale In France ? Ready to Ship!(Public)
By: GREENSPZOO
Wholesale Nutella Chocolate Available in Germany!(Public)
By: GREENSPZOO
Affordable Nutella Chocolate Available for Wholesale!(Public)
By: GREENSPZOO
The Benefits of Investing in Gold Bullion from African Suppliers(Public)
By: CEMAC
The Ultimate Guide to Gold and Diamond Trade in Africa: Suppliers, Prices, and Regulations(Public)
By: CEMAC
CEMAC Buyer Permit, CEMAC Permit, or Mandate License.(Public)
By: CEMAC
Regulation For Importing Africa Gold Into The U.S.: EUROPE: DUBAI Coins, Medals, and?Bullion(Public)
By: CEMAC