Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
Created September 25, 2022 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karl-gustav/001e05e70527986f8b6d11f675ed610c to your computer and use it in GitHub Desktop.
Save karl-gustav/001e05e70527986f8b6d11f675ed610c to your computer and use it in GitHub Desktop.
Get full URL from http request in go
func fullURL(r *http.Request, overridePath ...string) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if len(overridePath) != 0 {
return fmt.Sprintf("%s://%s%s", scheme, r.Host, overridePath[0])
}
return fmt.Sprintf("%s://%s%s?%s#%s", scheme, r.Host, r.URL.Path, r.URL.RawQuery, r.URL.Fragment)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment