ChatGPT解决这个技术问题 Extra ChatGPT

Is an entity body allowed for an HTTP DELETE request?

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 body of the request?

In ASP.NET WebApi 2 FromBody Parameters are ignored for HttpDelete endppoints.
I have a similar concern, but my case is different. I want to issue a batch delete request when I want to delete hundred objects. Certainly it is a great performance boost for pre HTTP 2.0 networks.
Have there been any changes in HTTP/2 ?

M
Matt G

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)


@Jason Definitely. You could also use custom headers to pass additional data, but why not use the request body.
Although the spec does not forbid DELETE requests from having a message-body, section 4.3 seems to indicate that the body should be ignored by servers since there are no "defined semantics" for DELETE entity-bodies: "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."
Please note that a lot of clients are also unable to send a DELETE with a body. This just burned me on Android.
Lots of discussion about implementation mixed with HTTP spec. Clients will implement things in the way they interpret the spec, do not confuse this with the meaning of the spec. The fact is that the spec leaves this ambiguous. I disagree with interpretation that because there is no defined semantic for entity-body that there is an implication that it should be ignored. I think people are working backwards from clients specific interpretations that exist (Jersey, Android test clients, etc.) and trying to justify the interpretation rather than attempting to be true to spec. Humans are fallible.
@Tomalak the language is a bit ambiguous, but the intent is not. I asked the authors and shelley (in this thread) is correct. The next version of the HTTP specification will be more precise.
M
Moshe Katz

The 2014 update to the HTTP 1.1 specification (RFC 7231) explicitly permits an entity-body in a DELETE request:

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.


Which version? Version 20 still has the same wording as version 19 I linked above: "Bodies on DELETE requests have no defined semantics. Note that sending a body on a DELETE request might cause some existing implementations to reject the request."
Version 26 suggestions that you can permit a body: 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. So it comes with a backward compatibility warning, it is suggesting that the next standard will be saying: 'yep! DELETE can have a body`.
RFC 7231 section 4.3.5 finalizes the language from version 26 with A payload within a DELETE request message has no defined semantics. So the body is allowed.
Body is allowed but should not be relevant to the request. There's absolutely no point in using it.
@Evert I disagree as well. A body in the DELETE request is useful if for example you want to instruct a server to remove multiple entities at once, with the ID's provided to the request via a payload in the body (passing these via URL query params might be too large)
a
abhi

Some versions of Tomcat and Jetty seem to ignore a entity body if it is present. Which can be a nuisance if you intended to receive it.


Google App Engine instantiates and passes an empty default entity instead of the request body.
N
Neil McGuigan

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;
    }
}

Having bodies in GET (and DELETE) is clearly mistreating HTTP and REST. There are other mechanisms for dealing with concurrency control (e.g. If-Modified-Since and etags).
How is it CLEARLY mistreating it when the spec does not forbid the body in DELETE?
Because you're not meant to do anything with the body. See: stackoverflow.com/a/983458/372643
This is exactly the same issue: GET allows you to retrieve the representation of the resource identified by the URI, and DELETE deletes the resource identified by the URI. Use a different URI for other versions if you want to delete specific versions. The URI should be the sole identifier of the resource in HTTP/REST. Use metadata in headers if you need do handle concurrency (e.g. If-Unmodified-Since or Etag, that's what they're for).
Use ETag header instead of a version field in a body
B
Ben Fried

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.


for whatever reason - because the spec says so :P
The spec doesn't "say so" it just says that the body isn't specifically defined. If it's not defined, and you want to ignore it, cool...go ahead and ignore it. But rejecting the request outright seems extreme and unnecessary.
Don't rely on undefined behavior. It's a pretty common best practice.
@Evert there's explicitly undefined behaviour (such as you see describe in the specifications of the C language for example) and there's behaviour that is permitted but simply not described. Using a message body in DELETE is the latter.
R
Roberto Polli

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.


C
Community

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).


C
CleverPatrick

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.


Notably they added back in after some discussion. See here for reference.
C
Community

I don't think a good answer to this has been posted, although there's been lots of great comments on existing answers. I'll lift the gist of those comments into a new answer:

This paragraph from RFC7231 has been quoted a few times, which does sum it up.

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.

What I missed from the other answers was the implication. Yes it is allowed to include a body on DELETE requests, but it's semantically meaningless. What this really means is that issuing a DELETE request with a request body is semantically equivalent to not including a request body.

Including a request body should not have any effect on the request, so there is never a point in including it.

tl;dr: Techically a DELETE request with a request body is allowed, but it's never useful to do so.


"semantically meaningless" doesn't mean the same as "has no defined semantics". The former means that it cannot have any meaning. The latter simply means that the RFC itself does not specify what those semantics might be. (I write RFCs)
In other words, if the implementor of an API desires to define some semantics for themselves, they're perfectly free to do so.
@Alnitak this is definitely a mis-interpretation. By that definition any HTTP request body has no defined semantics, but DELETE and GET are specifically called out in the specification. Here's a snippet from a yet-to-be-published draft talking about this specifically about the GET request:
If that's the case then 7231 is poorly worded, and should have said "the payload body MUST be ignored". Which draft are you referring to above?
@Alnitak my source tools.ietf.org/html/draft-ietf-httpbis-bcp56bis-06 I 100% agree with you and I think the current wording is also responsible for people adding request bodies to GET. The HTTP WG is working on a new version of th HTTP specification so maybe it's a good time to clean that language up a bit.
E
Eliezer Garza

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.


Using a POST is not a good RESTy way to create new resources because the semantics of POST responses are unclear, especially in the context of Location headers. You are essentially leaving HTTP behind and stack RPC on top. The proper "HTTP/REST way" is to create resources using PUT w/ the If-None-Match: * header (or spec'ing proper HTTP methods, see MKCOL etc).
I use POST to crete objects, PUT to update them and PATCH to make partial updates.POST /objects/deleteList doesnt make any sense for me, the "rest way" should be call DELETE /objects/{object_id} or DELETE /objects?objectNameLike={object_prefix} for example.
@hnh creating a new resource with PUT breaks the idempotent rule since you don't know the ID of the resource beforehand.
@JJCV settings the list of ids in the query string may work until you pass too many and exceed the url length limit, then you must move to a POST request. That's the main reason I know people want to have a body on DELETE.
@JesusTheHun If you are sending too many ids in a request, so many you may exceed the url lenght limit you are doing something very wrong in the first place. Sometimes people want things to circumvent their problems the easy way instead of addressing the problem itself.
M
Magnus Reftel

It seems ElasticSearch uses this: https://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-request-scroll.html#_clear_scroll_api

Which means Netty support this.

Like mentionned in comments it may not be the case anymore


If you use apache http client you can easily create your own versions of GET and DELETE by extending HttpEntityEnclosingRequestBase and making the getMethod() method return GET or DELETE. We use this for talking to elasticsearch.
dead link - great. we need more of those link answers - not
The linked documentation now contains only POST requests, no DELETEs. Might be worth adding a note to this answer?
Elasticsearch uses body with GET requests too.
C
Community

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://www.rfc-editor.org/rfc/rfc7231#page-29


Specifically, RFC 7231 section 4.3.5
This exact quote was already included in previous answers, this answer should be deleted.
O
Overclocked Skid

In case anyone is running into this issue testing, No, it is not universally supported.

I am currently testing with Sahi Pro and it is very apparent a http DELETE call strips any provided body data (a large list of IDs to delete in bulk, as per endpoint design).

I have been in contact with them several times, also sent three separate packages of scripts, images and logs for them to review and they still have not confirmed this. A failed patch, and a missed conference calls by their support later and I still haven't gotten a solid answer.

I am certain Sahi does not support this, and I would imagine many other tools follow suite.


It is implemented in the latest version of Sahi Pro. Since Sahi uses java to make HTTP calls, and Java had a bug prior to version 1.8 which won't let user make a DELETE request. So with Java 1.8 onwards and Sahi Pro 6.1.1(to be public soon), people can make DELETE request with body in Sahi.
E
Eneko

Practical answer: NO

Some clients and servers ignore or even delete the body in DELETE request. In some rare cases they fail and return an error.


M
Moshe Katz

Several other answers mention RFC 7231 which had effectively said that a DELETE request is allowed to have a body but it is not recommended.

In 2022, RFC 7231 was superseded by RFC 9110: HTTP Semantics, which now says:

[...] content received in a DELETE request has no generally defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request and close the connection [...]. A client SHOULD NOT generate content in a DELETE request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported. An origin server SHOULD NOT rely on private agreements to receive content, since participants in HTTP communication are often unaware of intermediaries along the request chain.

This language has been strengthened from the previous language, to say that even though it is allowed, you really need to be very careful when using it because (for example) some users might be behind a proxy that would strip the body from the request in order to combat "request smuggling".


A
Ashish

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