chi icon indicating copy to clipboard operation
chi copied to clipboard

Getting 'unexpected EOF' error frequently while reading r.Body()

Open nitiprabhu opened this issue 5 years ago • 0 comments

I have added a POST API using go-chi for particular use cases. While reading the payload from the request ( ioutil.ReadAll(r.Body) where r is r *http.Request as given below), I am getting 'unexpected EOF' error so frequently but I am unable to reproduce the same on testing environment.

Could any help to understand when will this error occur? Am i missing anything?

Code snippet:

	body, err := ioutil.ReadAll(r.Body)
	payloadSize := len(body)

	if err != nil {
		log.Error("Error while reading the payload, due to:", err)
		log.Error("Body:", string(body))
                log.Error("payloadSize:", payloadSize)
		http.Error(w, "Error while reading the payload", 400)
		return
	}

	if payloadSize == 0 {
		og.Error("size of the Payload is 0 byte")
		http.Error(w, "empty payload received", 400)
		return
	}

Sample error log:

Sample1

Error while reading the payload, due to: unexpected EOF Body:"" payloadSize:0

Sample2

Error while reading the payload, due to: unexpected EOF Body:"unfinished json string" payloadSize:24576

nitiprabhu avatar Aug 27 '21 17:08 nitiprabhu