Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading content from http.Get in Golang

Tags:

http

go

get

I'm having a tough time reading XML from a GET request in Go. I just started to learn Go and haven't found any resources on this topic. What I tried:

response, err := http.Get(url)
if err != nil {
   log.Fatal(err)
} else {
   defer response.Body.Close()
   xml, _ := ioutil.ReadAll(response.Body)
   if err != nil {
      log.Fatal(err)
   }
}

_, err := io.Copy(os.Stdout, response.Body) works but I'd like to store the XML for further processing. Any help is greatly appreciated.

like image 880
lean der Avatar asked Mar 10 '17 11:03

lean der


1 Answers

What you've tried is mostly good. Few things to improve it:

http.Get() returns an http.Response and an optional error. If there is no error, that only means that the HTTP GET operation succeeded, but the server might have responded with an error document. So you still have to check the response HTTP status code.

Also io.ReadAll() also returns an error (besides the read data), don't forget to check that too.

Let's wrap it in a function:

func getXML(url string) (string, error) {
    resp, err := http.Get(url)
    if err != nil {
        return "", fmt.Errorf("GET error: %v", err)
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return "", fmt.Errorf("Status error: %v", resp.StatusCode)
    }

    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", fmt.Errorf("Read body: %v", err)
    }

    return string(data), nil
}

Testing / using the above function:

if xmlStr, err := getXML("http://somehost.com/some.xml"); err != nil {
    log.Printf("Failed to get XML: %v", err)
} else {
    log.Println("Received XML:")
    log.Println(xmlStr)
}

Also note that it would be the same to get the content of any other responses, so it's worth not "encoding" the string conversion and return type. This one is more general:

func getContent(url string) ([]byte, error) {
    resp, err := http.Get(url)
    if err != nil {
        return nil, fmt.Errorf("GET error: %v", err)
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("Status error: %v", resp.StatusCode)
    }

    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, fmt.Errorf("Read body: %v", err)
    }

    return data, nil
}

Using this to get an XML doc:

if data, err := getContent("http://somehost.com/some.xml"); err != nil {
    log.Printf("Failed to get XML: %v", err)
} else {
    log.Println("Received XML:")
    log.Println(string(data))
}
like image 136
icza Avatar answered Sep 28 '22 04:09

icza