Code works fine but I get error in io.Copy "write tcp...: write: connection reset by peer"

Hi all,
I have a simple code that copies from a Response into ResponeWriter, my code below:

func AnswerToResponseWriter(resp *http.Response, rw http.ResponseWriter) (err error) {
	for key, values := range resp .Header {
		for _, value := range values {
			rw.Header().Add(key, value)
	rw.WriteHeader(resp .StatusCode)

	defer resp .Body.Close()
	size, err := io.Copy(rw, resp .Body)
	if err != nil {
		log.Println("Error in io.Copy (AnswerToResponseWriter). Error: " + err.Error())

In all my tests I get the response normally and my client get all the information. But if the body is big (several KB) I this error:
Error in io.Copy (AnswerToResponseWriter). Error: write tcp> write: connection reset by peer
Size variable in this case, is smaller than the real content. But client still gets all the data.

Do you know the reason of the problem?

Thank you!

The remote end hung up on you. This is not necessarily a problem with your program.

The client is also mine :slight_smile: It is a simple python program that sends a GET request and gets an answer, I know there were no problems in the client and client got all the body. It happens most of the time, and only if body is big. So I think there is something more about it…

It sounds like your client closes the connection before it has finished
reading the response. This may not be a serious error depending on your use

Can it close the connection before reading the full response, but then to have full response?
(It just seems very strange to me)

Remove this line.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.