How to convert an HTTP response body to a string in Go
This article describes how you can parse the response of a web request and access it as a string in Go.
It’s often necessary to inspect the contents of an HTTP response for debugging purposes. In Go, you can
use the io.ReadAll() function (or ioutil.ReadAll() in Go 1.15 and earlier) to
read the whole body into a slice of bytes and convert the byte slice to a string
with the string() function.
Here’s an example that shows this concept in action:
If you want to print the entire response including the headers, or just the
response headers (without the body), you may use the httputil.DumpResponse()
To exclude the response body from the output, change the boolean argument to
httputil.DumpResponse() from true to false. Note that a corresponding
method exists that is just like DumpResponse, but for dumping HTTP requests instead.