Golang http Get empty response.Body

11,649

Maybe you are getting an HTTP Response error.

http.Get will not return an error if, for example, the server answers with a 404 - Not Found, or 400 - Bad Request. It is because they are not exceptional behavior, they are valid responses from the server.

You should check resp.StatusCode and see that it is one in the 200 range.

You can check the different status codes in the package documentation.

This question suggests that there are some problems downloading from google drive with go, because they use wildcards in the url, maybe you can try using the url listed in the answer: https://googledrive.com/host/ID

Share:
11,649
EntilZha
Author by

EntilZha

Updated on June 27, 2022

Comments

  • EntilZha
    EntilZha almost 2 years

    I am running into a problem that for certain urls, code in Golang is not retrieving the expected content. I am not posting the actual url, but it has this form and is a link to a google drive file download https://docs.google.com/uc?id=somelongid&export=download. If I use wget to get the file it works fine. I also have ruby code which uses open() and it works as well. For some reason though Golang returns an empty buffer and no error. If I use this code to fetch some "ordinary" url like a static website it works as expected and returns non-empty response.Body. Below is code I extracted out of my project to simplify and narrow where the problem could be. Below is output.

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
        resp, err := http.Get("myurlishere")
        if err != nil {
            fmt.Println("Something went wrong")
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        fmt.Printf("Body: %v\n", body)
        fmt.Printf("Error: %v\n", err)
    }
    

    Output

    $go run main.go
    Body: []
    Error: <nil>
    
  • EntilZha
    EntilZha over 9 years
    Good idea. I am getting a 403 error, but I dont understand why. My equivalent Ruby code works as does wget on the exact same url. The only interesting thing from wget is a 302 redirect and a curious warning that makes me think maybe its that: Warning: wildcards not supported in HTTP.
  • EntilZha
    EntilZha over 9 years
    Combining your suggestion with this gives me the answer: stackoverflow.com/questions/18177419/…. Stick that in your answer and I can mark it answered