Is an entity body allowed for an HTTP DELETE request?

后端 未结 15 1582
长发绾君心
长发绾君心 2020-11-21 13:32

When issuing an HTTP DELETE request, the request URI should completely identify the resource to delete. However, is it allowable to add extra meta-data as part of the entity

相关标签:
15条回答
  • 2020-11-21 14:03

    It is worth noting that the OpenAPI specification for version 3.0 dropped support for DELETE methods with a body:

    see here and here for references

    This may affect your implementation, documentation, or use of these APIs in the future.

    0 讨论(0)
  • 2020-11-21 14:07

    This is not defined.

    A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.
    https://tools.ietf.org/html/rfc7231#page-29

    0 讨论(0)
  • 2020-11-21 14:09

    One reason to use the body in a delete request is for optimistic concurrency control.

    You read version 1 of a record.

    GET /some-resource/1
    200 OK { id:1, status:"unimportant", version:1 }
    

    Your colleague reads version 1 of the record.

    GET /some-resource/1
    200 OK { id:1, status:"unimportant", version:1 }
    

    Your colleague changes the record and updates the database, which updates the version to 2:

    PUT /some-resource/1 { id:1, status:"important", version:1 }
    200 OK { id:1, status:"important", version:2 }
    

    You try to delete the record:

    DELETE /some-resource/1 { id:1, version:1 }
    409 Conflict
    

    You should get an optimistic lock exception. Re-read the record, see that it's important, and maybe not delete it.

    Another reason to use it is to delete multiple records at a time (for example, a grid with row-selection check-boxes).

    DELETE /messages
    [{id:1, version:2},
    {id:99, version:3}]
    204 No Content
    

    Notice that each message has its own version. Maybe you can specify multiple versions using multiple headers, but by George, this is simpler and much more convenient.

    This works in Tomcat (7.0.52) and Spring MVC (4.05), possibly w earlier versions too:

    @RestController
    public class TestController {
    
        @RequestMapping(value="/echo-delete", method = RequestMethod.DELETE)
        SomeBean echoDelete(@RequestBody SomeBean someBean) {
            return someBean;
        }
    }
    
    0 讨论(0)
提交回复
热议问题