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 := "//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 : //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
+11.6k Golang : Covert map/slice/array to JSON or XML format
+10.7k Golang : Simple file scaning and remove virus example
+6.5k Golang : Create zip/ePub file without compression(use Store algorithm)
+10.9k Golang : Sort and reverse sort a slice of runes
+29.5k Golang : How to redirect to new page with net/http?
+15k Golang : Read a file line by line
+23.2k Golang : Generate MD5 checksum of a file
+8.5k Golang : How to get ECDSA curve and parameters data?
+9.7k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+4.4k Golang : Pad file extension automagically
+4.5k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+8.6k Golang : Qt Yes No and Quit message box example