I believe a 200 OK
with a response that contains an error code, and ideally an error message, is more suitable than an HTTP error code.
The client has issued a request to entity/{id}
, which exists. If an invalid ID was provided here, I could see the use of a 4xx error code. For example, a 404 could be used to indicate that there is no entity
with the ID {id}
. A 403 would indicate that the client doesn't have permission to access or modify the entity
with the ID {id}
. A 405 can be returned if the entity
with ID {id}
cannot be modified by the PUT or PATCH request the client sent. A 415 could be returned if the server was expecting valid JSON but received invalid JSON or something else entirely (such as XML). A 429 would indicate that the client is making too many requests in a given period of time, if you have rate limiting. All of these are about fundamental communication issues between the client and the server.
The problem here isn't about communication between the client and the server. The client has sent a well-formed and valid request object to the server to make a modification to an entity that the client has permission to modify. The HTTP request has been successfully sent from the client to the server and accepted. The definition of the 200 OK
status message is for successful HTTP requests, and since the server was able to receive and process the request, the HTTP request is successful.
Once the server begins working on the request, there is an error. Since the client is allowed to change the parent ID of the entity
, the receiving application can attempt to perform and update and then find that there is no valid parent ID.
If you absolutely can't return a 2xx error code, the next one that I consider would be a 409. The server could not fully process the request because of the state of the resources. That is, since there is no currently available entity with the specified parentId
, the entity could not be updated.
You also have questions that need to be resolved.
If you are allowing the content
and the parentId
to be updated, what do you do when one is valid and the other is invalid? If the parentId
doesn't exist, will you still update the content
of the entity
with ID {id}
? If you return a 4xx error code, I would not expect any aspect of the request to have succeeded. I would expect the entity
to remain unchanged, but you may opt to implement a system that updates the content
and returns a response indicating which field(s) were updated and which ones had errors (and what those errors were).
You may also want to consider what to do when the {id}
in the request endpoint and the id
field in the request body don't match. Perhaps this is a 4xx error.
parentId
in the request? Are you actually changing the parent ID because of this request?I fear using 404 Not Found could mislead the client to believe the target id is invalid. I also fear that using 400 Bad Request might imply the client sent a logically invalid payload
Both 404 and 400 can go along with a message. Don't just throw a 4xx error and hope for the best. Tell what made the request to be bad request or what wasn't found.parentId
can be changed in the request