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.
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.
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With