Home > Software engineering >  http.NewRequest allow only one redirect
http.NewRequest allow only one redirect

Time:10-07

I came from this question and the answer works if I want no redirect at all:

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

But how can I allow exactly one redirect here?

CodePudding user response:

As the documentation states:

The arguments req and via are the upcoming request and the requests made already, oldest first.

So at the first redirect, len(via) will be 1. If you return error if len(via)>1, it should fail for additional requests.

  •  Tags:  
  • go
  • Related