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
+9.1k Golang : automatically figure out array length(size) with three dots
+20.8k Golang : Saving private and public key to files
+8.3k Golang : Get final or effective URL with Request.URL example
+19.4k Golang : Populate dropdown with html/template example
+16.7k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+46.6k Golang : Encode image to base64 example
+8.8k Golang : Heap sort example
+20.6k Swift : Convert (cast) Int to int32 or Uint32
+9.3k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+8.5k Golang : Convert word to its plural form example
+8.1k Golang : Grayscale Image