Search code examples
httpgonet-http

How to execute a HEAD Request in GO?


I want to get the content length of a page using GO net/http? I can do this in terminal using curl -i -X HEAD https://golang.org and then check the content-length field.


Solution

  • use http.Head()

    res, err := http.Head("https://golang.org")
    if err != nil {
        panic(err)
    }
    contentlength:=res.ContentLength
    fmt.Printf("ContentLength:%v",contentlength)