Cory
Cory

Reputation: 15615

How to retrieve the final URL destination while using the http package in Go?

Most sites redirect to another URL during a request. For example: http://example.com might might redirects to http://mobile.example.com

Is there a way to retrieve the final destination? In case of cURL, they call this the effective URL.

Upvotes: 3

Views: 798

Answers (1)

peterSO
peterSO

Reputation: 166895

For example,

package main

import (
    "fmt"
    "net/http"
)

func main() {
    getURL := "http://pkgdoc.org/"
    fmt.Println("getURL:", getURL)
    resp, err := http.Get(getURL)
    if err != nil {
        fmt.Println(err)
        return
    }
    finalURL := resp.Request.URL.String()
    fmt.Println("finalURL:", finalURL)
}

Output:

getURL: http://pkgdoc.org/
finalURL: http://godoc.org/

Upvotes: 4

Related Questions