REST HTTP status code if DELETE impossible

前端 未结 2 1512
春和景丽
春和景丽 2021-01-30 20:09

My question is quite a generic one about HTTP status code when a DELETE is impossible on the resource (but not regarding user\'s rights).

We have a REST

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 20:13

    A 409 Conflict response is definitely wrong if the client can't resolve the conflict and delete the request later. That is, unless the resource has state tracking whether it can be deleted or not, 409 Conflict is not a good fit.

    A 403 Forbidden doesn't necessarily mean not authorized:

    However, a request might be forbidden for reasons unrelated to the credentials.
       -- RFC 7231

    The implication is usually there, though. You can use this code, but it may cause some confusion. It'll be especially tricky if the method actually requires authorization also - you'll need a code or something in the response indicating whether the failure was related to authorization or the resource being non-deletable.

    I think that 405 Method Not Allowed is the correct way to go.

    The 405 (Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource.
       -- RFC 7231

    The method DELETE is not supported for this resource. That sounds exactly like what you're describing. The HTTP spec doesn't really have a concept of a type of resource - just a resource. It happens that people group individual resources under the same endpoint for sanity, but that's just a convenience for developers and users. As far as the HTTP spec is concerned, /widgets/12 and /widgets/15 and /widgets/3453 are three different resources. The fact that the same object represents all three of those resources on the server is completely irrelevant. I think that's the "type" you're thinking of, but to HTTP that's just an implementation detail.

提交回复
热议问题