Search code examples
apiresthttphttp-response-codes

What HTTP response code should be returned when a constraint fails?


We have two objects called Track and Walkthru. They are linked to each other through a TrackWalkthruAssociation. If a Track and Walkthru are associated with each other (i.e., a TrackWalkthruAssociation instance exists linking them together), neither one can be deleted unless the TrackWalkthruAssociation instance that links them together, is deleted first.

So sending a DELETE to /tracks/1 or /walkthrus/1 for example, when an association exists, should fail. What would the appropriate HTTP response code be, for this? I don't want to return a 500 because this is not an unexpected condition.


Solution

  • Guess, it is OK to return: 409 ('Conflict').

    The 409 error response tells the client that they tried to put the REST API’s resources into an impossible or inconsistent state.

    In you case if you delete any of the linked resources before the link itself, you put resources into the impossible state.

    More details & guidelines can be found in RESTful Web APIs or REST API Design Rulebook