Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Created September 9, 2021 11:57
Show Gist options
  • Save nilsmagnus/0e25a58d9f6e452bab13e142e37886d3 to your computer and use it in GitHub Desktop.
Save nilsmagnus/0e25a58d9f6e452bab13e142e37886d3 to your computer and use it in GitHub Desktop.
follow redirects with go and collect all the cookies returned
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
// main, do not ignore errors like this in production
func main() {
url := "https://www.vinmonopolet.no/api/products/546402?fields=FULL"
cookies, err := followRedirectsWithCookies(url)
if err != nil {
log.Fatalf("Something bad happened, %v", err)
}
req, _ := http.NewRequest("GET", url, nil)
for _, c := range cookies {
req.AddCookie(c)
}
resp, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("got response\n\n\t%s", body)
}
func followRedirectsWithCookies(url string) ([]*http.Cookie, error) {
cookies := make([]*http.Cookie, 0)
transport := http.Transport{}
location := url
for {
req, err := http.NewRequest("GET", location, nil)
if err != nil {
return nil, err
}
for _, c := range cookies {
req.AddCookie(c)
}
resp, err := transport.RoundTrip(req)
if err != nil {
return nil, err
}
if resp.Cookies() != nil {
cookies = append(cookies, resp.Cookies()...)
}
if resp.StatusCode != 302 {
log.Printf("\nGot final url at %s", location)
break
} else {
location = resp.Header.Get("Location")
fmt.Printf("...%s", location)
if !strings.HasPrefix(location, "https") {
location = fmt.Sprintf("https://%s%s", req.Host, location)
}
}
}
return cookies, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment