HTTP client
Making HTTP requests is a core part of many applications these days. Go, being a web-friendly language, contains several tools for making HTTP requests in the net/http
package.
The basic HTTP request
This example uses the http.Get()
function from the net/http
standard library package. It will read the entire response body to a variable named body
and then print it to standard output:
package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { // Make basic HTTP GET request response, err := http.Get("http://www.example.com") if err != nil { log.Fatal("Error fetching URL. ", err) } // Read body from response body, err := ioutil.ReadAll(response.Body) response.Body.Close() if err != nil { log.Fatal("Error reading response. ", err) } fmt.Printf("%s\n", body) }
Using the client SSL certificate
If a remote HTTPS server has strict authentication and requires a trusted client certificate, you can specify the certificate...