Golang : Get final or effective URL with Request.URL example
There are times when we query a web server, it will re-direct internally and returns back with a slightly different URL structure. The returned URL is known as final URL or "effective url". In this following example, we will learn how to use https://golang.org/pkg/net/http/#Request.URL to retrieve the final URL.
package main
import (
"fmt"
"net/http"
)
func main() {
// without https (SSL)
originalURL := "http://socketloop.com"
resp, err := http.Get(originalURL)
if err != nil {
fmt.Println(err)
}
// if there is any re-direction happening behind the scene
// the finalURL will be different
// in this case, there will be a re-direction to https (SSL) version
finalURL := resp.Request.URL.String()
fmt.Println("Original URL is : ", originalURL)
fmt.Println("Final URL is : ", finalURL)
}
Socketloop.com's NGINX web server will automatically redirect all non-https
request to https(SSL)
. Therefore, the final URL will be SSL-ed connection.
Original URL is : http://socketloop.com
Final URL is : https://socketloop.com/
References :
https://www.socketloop.com/tutorials/golang-http-get-example
See also : Golang : Intercept and compare HTTP response code example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+32.8k Delete a directory in Go
+4.8k Python : Convert(cast) bytes to string example
+13.9k Elastic Search : Mapping date format and sort by date
+11.4k CodeIgniter : Import Linkedin data
+19.6k Golang : How to get time from unix nano example
+13.6k Golang : convert(cast) string to float value
+4.9k Python : Create Whois client or function example
+8.7k Golang : Populate or initialize struct with values example
+14.8k Golang : How to check if IP address is in range
+4.6k Which content-type(MIME type) to use for JSON data