Search code examples
restjerseyjax-rshttp-error

API Development: When to return a BAD REQUEST HTTP Status Code (REST)


We're authoring REST services, and there's a debate on what to do when someone requests a resource with a parent ID that does not exist.

Example: You are asking for a list of people associated with a company, so you GET with ID 1, but that company ID does not exist.

I would argue that the definition of REST says that we would simple return a empty list (resulting in a HTTP 204 (No Content)), since a HTTP Bad Request is for only malformed syntax, per the specification:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

I think it's also clearer that there was no error to be interpreted, the resource you are requesting does not exist.

Thoughts on best practice?

There is a SO discussion here: HTTP 400 (bad request) for logical error, not malformed request syntax although it's a bit more abstract, I'm torn if I should post this, or simply use that question.


Solution

  • If you do

    GET /company/1
    

    and there does not exist a company with id 1 then I think the appropriate HTTP status code is 404 - not found.

    However, if you were to do,

    GET /companies?id=1
    

    Then I would return a 200 and an empty list of companies.