A body is optional for all the 4xx
error codes:
Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
Same for the 5xx
:
Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation...
(SHOULD means that really, they should, but it's not a MUST.)
The 1xx
messages don't contain a body as far as I can tell. 201
(in addition to 204
and 205
) doesn't have to have a body. The 3xx
are a mixed bag, but a body is optional for a lot of them.
Source: HTTP/1.1: Status Code definitions.
The rules for message length information are described in HTTP Message - 4.4 Message Length. It's not exactly trivial.