Am I the only person to get annoyed when people don’t use HTTP status codes when they build an API?
I’m working with an API at the moment. The API has been built using Rails, which is fine by me. However, I just posted some data and got the following:
response => <Net::HTTPOK 200 OK readbody=true>
response.body
=> "{\"status\":\"ERROR\",\"result\":\"failure\",\"message\":\"Lines.description can't be blank\"}"
What! The call has obviously failed. So why is the HTTP status code set to 200!
The response should have been a 400 Bad Request. If that was the case, I wouldn’t have to dig around in the body of the response to determine whether the request had been successful or not.
The HTTP protocol has a host of HTTP status codes, for providing a rich vocabulary of responses to a request. It is so annoying when API developer/designers ignore that richness.
Strictly speaking JSON APIs shouldn’t have HTTP “header” responses of any kind IMO. It’s why you end up with the situation you have here. Header responses are designed for HTTP requests, but JSON is a stand alone format for a response and should not depend or be intermingled with HTTP headers.
As far as standardization of errors I do like using the same codes from HTTP standards but placing them within the JSON result. Regardless it’s best to follow compatible standards such as using JSONapi.org
@danielpclark, I think you are confusing HTTP and HTML. JSON API work on top of HTTP. You POST a new JSON object, and GET a list of JSON resources. You PUT a change to a JSON object, and DELETE a JSON object. POST, GET, PUT, and DELETE are all HTTP actions.
When a web page is delivered, it is an HTML document being delivered over HTTP. With a JSON API, it is the HTML element that is replaced, and not the HTTP. It’s JSON over HTTP.
@PikachuEXE a good point well presented. I agree, 422 may well be a better response than 400 in this example. However, I think that further emphasises the richness of HTTP status codes.
What I’m referring to is the fact that you can query response.headers in Rails (I’m assuming response.status is derived from them). In my opinion with JSON there should be none of those for response status. The status codes and response should be completely within the JSON response. The fact that resonse.headers includes a status result with JSON responses can lead to confusion. I’d rather the JSON API be responsible for the requests status codes and have the added benefit of its own additional error codes.
HTTP status codes is what I agreed would be nice to use in the JSON response.
Seeing what you pointed to
I suppose that is the result of the ErrorSerializer module shown there. I guess your original post points out the lack of this proper implementation. The serializer seems to solve the issue.