35 lines
700 B
Go
35 lines
700 B
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidStatusCode = errors.New("invalid status code")
|
|
)
|
|
|
|
// GetRedirect returns the final URL after redirection.
|
|
func GetRedirect(url string) (string, error) {
|
|
const op = "api.GetRedirect"
|
|
|
|
client := &http.Client{
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse // stop after 1st redirect
|
|
},
|
|
}
|
|
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusFound {
|
|
return "", fmt.Errorf("%s: %w: %d", op, ErrInvalidStatusCode, resp.StatusCode)
|
|
}
|
|
|
|
return resp.Header.Get("Location"), nil
|
|
}
|