Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 453
Vivin Paliath Avatar asked Oct 23 '14 18:10

Vivin Paliath


People also ask

What is the 409 response code?

The HTTP 409 status code (Conflict) indicates that the request could not be processed because of conflict in the request, such as the requested resource is not in the expected state, or the result of processing the request would create a conflict within the resource.

What is a 201 response code?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

Which HTTP codes should retry?

HTTP status codes and the error message can give you a clue. In general, a 5xx status code can be retried, a 4xx status code should be checked first, and a 3xx or 2xx code does not need retried.


1 Answers

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

like image 80
Grigorii Chudnov Avatar answered Oct 22 '22 01:10

Grigorii Chudnov