Why close the HTTP API response body in Golang? What if you don't...

Why close the HTTP API response body in Golang? What if you don't...

In the previous article, we saw how to make the HTTP GET API request in Golang. While going through the example, we discussed closing the response body.

In this article, Let's discuss why it is essential to close the response body.

What is the response body?

The response body is a stream of data that is read from the server.

Why should I close it?

  • Ensure all data has been read and the resources associated with it are freed up.

What happens if I don't close it?

  • It can lead to resource leaks.

  • It can cause the program to consume more memory and resources than necessary, leading to performance issues.

  • It can also cause issues with the underlying connection as transport may not reuse HTTP/1.x “keep-alive” TCP connections if the Body is not read to completion and closed.

From the official docs,

Link: https://pkg.go.dev/net/http#Client

    // The http Client and Transport guarantee that Body is always
    // non-nil, even on responses without a body or responses with
    // a zero-length body. It is the caller's responsibility to
    // close Body. The default HTTP client's Transport may not
    // reuse HTTP/1.x "keep-alive" TCP connections if the Body is
    // not read to completion and closed.

What should I do?

  • Always use defer statement and write down the statement immediately. This will ensure that the response body gets closed even in case of runtime error during the reading and parsing of the response.

Here's the complete example,

    resp, err := http.Get("http://localhost:8080")
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

Thanks for reading :)

This blog post has a more detailed explanation for the same.

I hope you learned something new from this blog post. Click here to learn about me and how you can support my work, Thank you.

Did you find this article valuable?

Support Suraj Narwade by becoming a sponsor. Any amount is appreciated!