Once you ship an HTTP request to a server, you typically need to learn the contents of the response. In Go, the physique of the response will be discovered within the Response.Physique
area of the http.Response
object, which is returned as the results of sending a request by an HTTP consumer. The HTTP response physique in Go is of kind io.ReadCloser
. To transform the io.ReadCloser
object to a string, that you must learn the contents of this area utilizing the io.ReadAll()
operate.
|
|
Within the instance above, we’re making an ordinary GET
request to the https://instance.com/ web site. On line 21
, we learn the response physique with the io.ReadAll()
operate. This operate returns the learn response physique as a slice of bytes, so to be able to print it, we have to convert it to a string.
Because the output of the instance, you will note the HTML content material of the https://instance.com/ web site:
<!doctype html>
<html>
<head>
<title>Instance Area</title>
<meta charset="utf-8" />
<meta http-equiv="Content material-type" content material="textual content/html; charset=utf-8" />
<meta title="viewport" content material="width=device-width, initial-scale=1" />
<model kind="textual content/css">
physique {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:hyperlink, a:visited {
shade: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</model>
</head>
<physique>
<div>
<h1>Instance Area</h1>
<p>This area is for use in illustrative examples in paperwork. You could use this
area in literature with out prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/instance">Extra info...</a></p>
</div>
</physique>
</html>