DEV Community

fuchiao
fuchiao

Posted on

http redirect for golang http client

I had a problem today.

I wrote a program to send an http request to my server, and the debugger told me that the http server returned 302. However, my client program always got 400.

today i learned

Finally, I found that golang http client ignoreed 301 and 302. The client redirected to the new location and returned the new response only.

Thanks, wireshark.

And we can prevent redirects by the following configuration:

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

  resp, err := client.Get("http://www.jonathanmh.com")

reference

[1] https://jonathanmh.com/tracing-preventing-http-redirects-golang/

Top comments (0)