0

I'm wondering which is the best choice for the HTTP response code in the following situation:

There is a POST / PUT REST API that associates a resource (let's call it source item) with another one (let's call it destination item), using a 1-1 requirement (one-to-one).

If the source item is already associated with another resource, the API should return an error. If the source item is not already associated with any resource, but the destination one is, the API should succeed instead by replacing the current association of the destination.

I'm going with 409 Conflict, but since it indicates a conflict in the target resource, I'm not sure if it is the most appropriate.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409

1
  • is it a client error? Why wouldn't the client know the resource is already associated with something else? How did the client reach the state of being allowed to make that request? Why can the source item override an existing association? i.e. telling the destination not to link to something else but link to the source instead? Commented Dec 5, 2023 at 15:19

1 Answer 1

1

My interpretation is that the client shouldn't send such request as source item should not be binded to any destination item beforehand.
So the status code is effectively from 4xx family.

Reading the MDN documentation of 409 Conflict does not give a lot of explanations but you might find similarities with your context. The request to bind source and destination entities does conflict with the current state of your source entity (as it's already binded).

To me, your server should repsond 409 Conflict with a detailed explanation on how to solve your problem (unbind source entity of binded destination entity).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.