I’m messing around with Go’s HTTP package and am sending requests. I’d like to send a user agent header:
package main
import ( "net/http"; "fmt"; "io/ioutil" )
func main() {
resp, err := http.Get("https://www.whatsmyua.info/")
if err != nil {
fmt.Println("Error: ", err)
}
defer resp.Body.Close()
resp.Header.Set("User-Agent", "Riverside Rocks")
for k, v := range resp.Header {
fmt.Printf("%s: %s\n", k, v)
}
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("\nResponse Body:\n%s\n", body)
}
This code works as expected (it returns the HTML of a website), but the user agent is still “go-http-client”. How can I fix this?