hariprasadraja
1/20/2020 - 10:48 AM

go http check redirect

// file: http-redir-loop.go
package main

import(
  "fmt"
  "net/http"
)

func main(){
  myURL := "http://www.jonathanmh.com"
  nextURL := myURL
  var i int
  for i < 100 {
    client := &http.Client{
      CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    } }

    resp, err := client.Get(nextURL)

    if err != nil {
      fmt.Println(err)
    }

    fmt.Println("StatusCode:", resp.StatusCode)
    fmt.Println(resp.Request.URL)

    if resp.StatusCode == 200 {
      fmt.Println("Done!")
      break
    } else {
      nextURL = resp.Header.Get("Location")
      i += 1
    }
  }
}