简体   繁体   中英

What is the correct http status code to return when a DELETE cannot be performed?

My application accepts http DELETE requests which will remove entries from a database managed through hibernate.

The removal of certain entries would produce a hibernate.ConstraintViolationException because they are key in another table, thus they are not accepted. However, this error can change in time depending of the status of the DB.

What is the correct http response for such a scenario?

I thought of 412 (Precondition Failed) because the precondition of the entry being not in use by the system is not met.

This sounds a lot like the client would not have the powers to correct that error if it occurs. That alone is something that would qualify for the 5xx-range of status codes:

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. […] These response codes are applicable to any request method.

To be precise, I think 503 (Service Unavailable) is in order here. That code is mostly known for indicating maintenance, but it is really indicating a temporary state on the server side that is preventing the request to be fulfilled. This would also be in line with this answer .

If you are uncomfortable with this, here are some alternatives in order of recommendation:

However, if the client has a chance to correct this error (eg by issuing another request first), the 5xx-class is out of the question and you should start with code 424. If you are concerned with this code being introduced by WebDAV, don't worry: It is listed in the IANA HTTP Status Code Registry and therefor valid in HTTP.

Did you consider 409? -- "The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict." -- https://greenbytes.de/tech/webdav/rfc7231.html#status.409

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM