Sunday, May 5, 2024
HomeGolangConvert HTTP response io.ReadCloser to string in Go (Golang)

Convert HTTP response io.ReadCloser to string in Go (Golang)



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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package deal predominant

import (
    "fmt"
    "io"
    "log"
    "internet/http"
)

func predominant() {
    consumer := &http.Shopper{}
    req, err := http.NewRequest(http.MethodGet, "https://instance.com/", nil)
    if err != nil {
        log.Deadly(err)
    }
    resp, err := consumer.Do(req)
    if err != nil {
        log.Deadly(err)
    }

    bytes, err := io.ReadAll(resp.Physique)
    if err != nil {
        log.Deadly(err)
    }

    fmt.Println(string(bytes))
}

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>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments