https:\/\/golang.org\/src\/net\/http\/client.go<\/a><\/p>\n\n\n\nhttp.Client<\/strong> lets you specify a policy of how redirects can be handled. Below is the structure of http.Client <\/strong>struct<\/p>\n\n\n\ntype Client struct {\n\tTransport RoundTripper\n\tCheckRedirect func(req *Request, via []*Request) error\n\tJar CookieJar\n\tTimeout time.Duration\n}<\/code><\/pre>\n\n\n\nThe CheckRedirect<\/strong> field is a function actually which lets you specify a way of how redirects can be handled. This function is called before following any future redirects. So basically whenever the server responds with a redirect the client will first call the CheckRedirect<\/strong> function to check if redirect should be followed or not. Below is the signature of the CheckRedirect<\/strong> function<\/p>\n\n\n\nfunc(req *Request, via []*Request) error<\/code><\/pre>\n\n\n\nThe first parameter req<\/strong> is the upcoming request<\/li><\/ul>\n\n\n\nThe second parameter via<\/strong> is the requests already made. That is why it is a slice. The first element is the oldest request.<\/li><\/ul>\n\n\n\nThere are two cases to consider<\/p>\n\n\n\n
<\/span>CheckDirect is nil<\/strong><\/span><\/h1>\n\n\n\nYou don’t specify the CheckRedirect field during the initialization of http.Client<\/strong> struct. In this case, default behavior kicks in, and the HTTP client will follow 10 redirects, and then it will return an error. Below is the defaultCheckRedirect<\/strong> function which quits after 10 retries.<\/p>\n\n\n\nfunc defaultCheckRedirect(req *Request, via []*Request) error {\n\tif len(via) >= 10 {\n\t\treturn errors.New(\"stopped after 10 redirects\")\n\t}\n\treturn nil\n}<\/code><\/pre>\n\n\n\n<\/span>CheckDirect is not-nil<\/strong><\/span><\/h1>\n\n\n\nYou specify your own CheckRedirect<\/strong> function during the initialization of the http.Client<\/strong> struct. When you specify this function, then this function will be called before following any future redirect. When you write your own CheckRedirect function you need to know the below things in mind When the CheckRedirect<\/strong> function returns an error, then the HTTP client will return two things<\/p>\n\n\n\nThe previous response along with body closed<\/li><\/ul>\n\n\n\nThe actual error (wrapped in a url.Error)<\/strong><\/li><\/ul>\n\n\n\n<\/strong>There is also a special case when CheckRedirect<\/strong> function returns ErrUseLastResponse <\/strong>error. In this case, the client will return<\/p>\n\n\n\nMost recent response along with body unclosed<\/li><\/ul>\n\n\n\nNon-nil error<\/li><\/ul>\n\n\n\nSo basically when you don’t want HTTP Client to follow the redirect, then please specify your own CheckRedirect<\/strong> function to control the behavior the way you want. For example below is one sample CheckRedirect <\/strong>function that will inform HTTP client to not follow any redirect and also not return any error, just return the last response along with body unclosed.<\/p>\n\n\n\nfunc MyCheckRedirect(req *Request, via []*Request) error {\n\treturn http.ErrUseLastResponse\n}<\/code><\/pre>\n\n\n\nHere is the definition of ErrUseLastResponse <\/strong>in the http package<\/p>\n\n\n\nvar ErrUseLastResponse = errors.New(\"net\/http: use last response\")<\/code><\/pre>\n\n\n\nCreate http.Client<\/strong> as below specifying the CheckRedirect<\/strong> function<\/p>\n\n\n\nclient := &http.Client{\n CheckRedirect: func(req *Request, via []*Request) error {\n\treturn http.ErrUseLastResponse\n },\n}<\/code><\/pre>\n\n\n\nHere is another sample CheckRedirect <\/strong>function which will only follow redirect two times and then return the error<\/p>\n\n\n\nfunc MyCheckRedirect(req *Request, via []*Request) error {\n\tif len(via) >= 2 {\n\t\treturn errors.New(\"stopped after 10 redirects\")\n\t}\n\treturn nil\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewCheckDirect is nilCheckDirect is not-nil Overview http.Client struct is used to make HTTP requests in golang. https:\/\/golang.org\/src\/net\/http\/client.go http.Client lets you specify a policy of how redirects can be handled. …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[314],"class_list":["post-5198","post","type-post","status-publish","format-standard","hentry","category-tech","tag-redirect"],"yoast_head":"\n
HTTP Client to not follow redirect in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n