7

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.

2
  • 1
    How is this related to XML? Commented Nov 11, 2018 at 13:33
  • @ChristianHujer you're right, actually it is not at all. I've edited the question accordingly. Thanks for your comment. Commented Nov 13, 2018 at 10:39

1 Answer 1

25

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))
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the detailed reponse! That really saved my day. I couldn't upvote the answer because of too less reputation - sorry for that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.