Chapter 3
3.3. Errors
Things can go wrong; a call to a wrong API endpoint, use of a wrong request method (like using GET instead of POST), missing parameters, wrong authentication details, etc. When this happens, the REST API should let you know. In most cases, the REST API will send back an error code and message. Most REST APIs use the standard HTTP status codes we talked about earlier. They are returned in the status line with the detailed error message returned as the response body.
Here is an example with the 500px1 API
500px API returns a 401 Unauthorized
HTTP status and a JSON formatted error message with the 401 error code and the exact error reason. APIs that return standard HTTP status codes in the response status line may also include non-standard codes in the response body. An example is Twitter. A request to an API resource that does not exist will return a 404 HTTP status code in the status line but a code 34 in the JSON response:
Some APIs don’t use standard HTTP status codes either. When there are errors, the HTTP status still reads 200 OK
but the result body will contain the non-HTTP code and detailed message. An example is Flickr.
When developing API solutions, you need to make allowance for these error cases. You need to understand how errors are returned by the provider and build to be able to capture and interpret this to your users in a subtle way.
-
500px.com is a photo sharing and discovery community. The API documentation is available on Github here: https://github.com/500px/api-documentation ↩