From the documentation it states that
For server requests the Request Body is always non-nil but will return EOF immediately when no body is present.
For ContentLength, the documentation states
For client requests, a value of 0 means unknown if Body is not nil.
So is it better to check for ContentLength
r *http.Request
if r.ContentLength == 0 {
//empty body
}
or to check EOF
type Input struct {
Name *string `json:"name"`
}
input := new(Input)
if err := json.NewDecoder(r.Body).Decode(input); err.Error() == "EOF" {
//empty body
}
Content-Length
is a header that the client can or cannot set. – Lapio.EOF
then doif err == io.EOF
. Looking at the string representation is at best incredibly fragile. – Jewel