Is an entity body allowed for an HTTP DELETE request?

后端 未结 15 1555
长发绾君心
长发绾君心 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 13:48

    Just a heads up, if you supply a body in your DELETE request and are using a google cloud HTTPS load balancer, it will reject your request with a 400 error. I was banging my head against a wall and came to found out that Google, for whatever reason, thinks a DELETE request with a body is a malformed request.

    0 讨论(0)
  • 2020-11-21 13:49

    Might be the below GitHUb url will help you, to get the answer. Actually, Application Server like Tomcat, Weblogic denying the HTTP.DELETE call with request payload. So keeping these all things in mind, I have added example in github,please have a look into that

    https://github.com/ashish720/spring-examples

    0 讨论(0)
  • 2020-11-21 13:51

    Using DELETE with a Body is risky... I prefer this approach for List Operations over REST:

    Regular Operations

    GET /objects/ Gets all Objects

    GET /object/ID Gets an Object with specified ID

    POST /objects Adds a new Object

    PUT /object/ID Adds an Object with specified ID, Updates an Object

    DELETE /object/ID Deletes the object with specified ID

    All Custom actions are POST

    POST /objects/addList Adds a List or Array of Objects included in body

    POST /objects/deleteList Deletes a List of Objects included in body

    POST /objects/customQuery Creates a List based on custom query in body

    If a client doesn't support your extended operations they can work in the regular way.

    0 讨论(0)
  • 2020-11-21 13:59

    The spec does not explicitly forbid or discourage it, so I would tend to say it is allowed.

    Microsoft sees it the same way (I can hear murmuring in the audience), they state in the MSDN article about the DELETE Method of ADO.NET Data Services Framework:

    If a DELETE request includes an entity body, the body is ignored [...]

    Additionally here is what RFC2616 (HTTP 1.1) has to say in regard to requests:

    • an entity-body is only present when a message-body is present (section 7.2)
    • the presence of a message-body is signaled by the inclusion of a Content-Length or Transfer-Encoding header (section 4.3)
    • a message-body must not be included when the specification of the request method does not allow sending an entity-body (section 4.3)
    • an entity-body is explicitly forbidden in TRACE requests only, all other request types are unrestricted (section 9, and 9.8 specifically)

    For responses, this has been defined:

    • whether a message-body is included depends on both request method and response status (section 4.3)
    • a message-body is explicitly forbidden in responses to HEAD requests (section 9, and 9.4 specifically)
    • a message-body is explicitly forbidden in 1xx (informational), 204 (no content), and 304 (not modified) responses (section 4.3)
    • all other responses include a message-body, though it may be of zero length (section 4.3)
    0 讨论(0)
  • 2020-11-21 14:00

    Roy Fielding on the HTTP mailing list clarifies that on the http mailing list https://lists.w3.org/Archives/Public/ietf-http-wg/2020JanMar/0123.html and says:

    GET/DELETE body are absolutely forbidden to have any impact whatsoever on the processing or interpretation of the request

    This means that the body must not modify the behavior of the server. Then he adds:

    aside from the necessity to read and discard the bytes received in order to maintain the message framing.

    And finally the reason for not forbidding the body:

    The only reason we didn't forbid sending a body is because that would lead to lazy implementations assuming no body would be sent.

    So while clients can send the payload body, servers should drop it and APIs should not define a semantic for the payload body on those requests.

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

    It appears to me that RFC 2616 does not specify this.

    From section 4.3:

    The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers. A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests. A server SHOULD read and forward a message-body on any request; if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

    And section 9.7:

    The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server. The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. However, the server SHOULD NOT indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location.

    A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

    If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.c

    So it's not explicitly allowed or disallowed, and there's a chance that a proxy along the way might remove the message body (although it SHOULD read and forward it).

    0 讨论(0)
提交回复
热议问题