Downloading and storing a zip file

I’m working on collecting log files from a server and storing them locally for additional processing. The logs come down as a zip. If I run this code, it does create the file, however it’s empty. If I take the url and paste it into a browser it does download the complete file. Any suggestions on how to fix it? I’m not worried about expanding the file, just want to download and store.

‘’'func DownloadFile(filepath string, url string) error {

request, httpErr := http.NewRequest("GET", url, nil)

// ignore certs during data collection
tr := &http.Transport{
	TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if httpErr != nil {
	fmt.Printf("The HTTP request failed with error %s\n", httpErr)
}
client := &http.Client{Transport: tr}

response, err := client.Do(request)

defer response.Body.Close()

// Create the file
out, err := os.Create(filepath)
if err != nil {
    return err
}
defer out.Close()

// Write the body to file
_, err = io.Copy(out, response.Body)
return err

}
‘’’

Thanks,
Kevin

1 Like

Hi, Kevin, I suspect the issue might be that you’re not getting a 200 result from the request. A non-200 does not return an error from (*Client).Do, per the documentation here. After calling client.Do, check the response.Status or response.StatusCode, etc.

1 Like

Hi Sean,

it was that and an auth issue, thanks for the help!!

Kevin

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.