How to check whether a URL is downloadable or not in golang?

Issue

I’m trying to download a file fro the url to a local file.

I wanted to test whether the requesting url is only file, if it is not a file it should return bad request

Any help could be appreciated

package main
    
    import (
        "fmt"
        "io"
        "net/http"
        "os"
    )
    
    func main() {
        fileUrl := "http://example.com/file.txt"
        err := DownloadFile("./example.txt", fileUrl)
        if err != nil {
            panic(err)
        }
        fmt.Println("Downloaded: " + fileUrl)
    }
    
    // DownloadFile will download a url to a local file.
    func DownloadFile(filepath string, url string) error {
    
        // Get the data
        resp, err := http.Get(url)
        if err != nil {
            return err
        }
        defer resp.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, resp.Body)
        return err
    }

Solution

Below is the way to check whether the URL is downloadable or not. Hope this helps someone 🙂

package main
            
import (
    "fmt"
    "io"
    "net/http"
    "os"
)
            
func main() {
    fileUrl := "http://example.com/file.txt"
    err := DownloadFile("./example.txt", fileUrl)
    if err != nil {
        panic(err)
    }
    fmt.Println("Downloaded: " + fileUrl)
}
            
// DownloadFile will download a url to a local file.
func DownloadFile(filepath string, url string) error {
            
    // Get the data
    resp, err := http.Get(url)
    contentType = resp.Header.Get("Content-Type")  
        
    if err != nil {
         return err
    }
    defer resp.Body.Close()
        
    if contentType == "application/octet-stream" {
        // 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, resp.Body)
        return err
    } else {
        fmt.Println("Requested URL is not downloadable")
        return nil
    }
}

Answered By – meena sushanth

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.