How to get URL in http.Request

From the documentation of net/http package:

type Request struct {
   ...
   // The host on which the URL is sought.
   // Per RFC 2616, this is either the value of the Host: header
   // or the host name given in the URL itself.
   // It may be of the form "host:port".
   Host string
   ...
}

Modified version of your code:

func Handler(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("Req: %s %s\n", r.Host, r.URL.Path) 
}

Example output:

Req: localhost:8888 /

Leave a Comment