Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an HTTP PUT request required to include a body?

Tags:

http

I'm having trouble finding a definite specification of this in the standard. I have an HTTP client that's not including a Content-Length: 0 header when doing a PUT request where I don't specify a body, and a server that gets confused by such requests, and I'm wondering which program I should be blaming.

like image 574
Marijn Avatar asked Aug 05 '09 13:08

Marijn


People also ask

Does PUT request have a response body?

PUT should never return a body, but must return a response code in the header. Just choose 200 if it was successful, and 4xx if not.

Can you send body on PUT request?

The HTTP PUT method is defined as idempotent, which means that multiple identical HTTP PUT requests should have the same effect as a single request. You can send data to the server in the body of the HTTP PUT request.

Is body mandatory in post request?

Yes it is expected to have Body/Content in body, but it is not required(Mandatory).

Can HTTP request have empty body?

In some scenarios, when requests don't include all required HTTP headers and flags, request bodies can now be detected as having an empty body and report a failure to the client.


1 Answers

HTTP requests have a body if they have a Content-Length or Transfer-Encoding header (RFC 2616 4.3). If the request has neither, it has no body, and your server should treat it as such.

That said it is unusual for a PUT request to have no body, and so if I were designing a client that really wanted to send an empty body, I'd pass Content-Length: 0. Indeed, depending on one's reading of the POST and PUT method definitions (RFC 2616 9.5, 9.6) one might argue that the body is implied to be required - but a reasonable way to handle no body would be to assume a zero-length body.

like image 188
bdonlan Avatar answered Sep 29 '22 22:09

bdonlan