Questions: Redirects
Hello.
There is a hook: Session.CheckRedirect. I have tried assigning a function but it never triggered.
The only way I managed to catch a redirect was with the help of CallbackWithContext function.
There was a response body of { redirectURL: someUrl }. The redirect happens but Session.Dump doesn't register that.
1.) Could you provide an example of using Session.CheckRedirect function? Maybe I was using it wrong.
2.) Session.Dump doesn't register redirects, so no logs of those?
3.) Content-Length header is omitted in Session.Dump?
Hello,
For CheckRedirect function, you need to use it as follow
session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
fmt.Println(req.Url)
return nil
}
~~There is indeed a bug on the Dump function that needs to be fixed.~~
Could you please send me the part of the code that doesn't work ?
I have tested with the following. It works.
session := azuretls.NewSession()
defer session.Close()
session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
fmt.Printf("\n[Url]: %+v [IsRedirected]: %+v [StatusCode]: %+v\n", req.Url, req.IsRedirected, req.Response.StatusCode)
return nil
}
session.CallbackWithContext = func(ctx *azuretls.Context) {
fmt.Printf("\n[Url]: %+v [IsRedirected]: %+v [StatusCode]: %+v\n", ctx.Request.Url, ctx.Request.IsRedirected, ctx.Response.StatusCode)
return
}
response, err := session.Get("https://twitter.com")
if err != nil {
panic(err)
}
fmt.Printf("\n[x.com status code]: %+v [IsRedirected]: %+v\n", response.StatusCode, response.Request.IsRedirected)
I believe that page I am working with has no redirect. Page's javascript does the thing after receiving the response from fetch. CallBackWithContext reports StatusCode 200 also and nothing else happens.
But while testing I have noticed two moments:
CallBackWithContext: [Url]: https://twitter.com [IsRedirected]: false [StatusCode]: 302
CheckRedirect: [Url]: https://x.com/ [IsRedirected]: false [StatusCode]: 302
CallBackWithContext: [Url]: https://x.com/ [IsRedirected]: false [StatusCode]: 200
No hooks: [x.com status code]: 200 [IsRedirected]: false
1.) IsRedirected property always false. 2.) CheckRedirect reports url it is about to redirect? While CallBackWithContext reveals the original. Not sure which part is intentional though.
-
IsRedirectedis indeed an unused property ofazuretls.Session, don't rely on it. - Yes,
CheckRedirectis indeed used to modify redirected requests only. To abort and return directly the response, you can return the errorazuretls.ErrUseLastResponseinCheckRedirectfunction:
session := azuretls.NewSession()
defer session.Close()
var called bool
session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
if req.Response == nil {
t.Error("expected non-nil Request.Response")
}
called = true
return azuretls.ErrUseLastResponse
}
response, err := session.Get("https://httpbin.org/redirect/1")
// the error will be nil: azuretls.ErrUseLastResponse is considered as a non real error
if err != nil {
panic(err)
}
if response.StatusCode != 302 {
panic("Request failed, expected: 302, got: ", response.StatusCode)
}
if !called {
panic("Request failed, CheckRedirect was not called")
}
CallBackWithContext method is called after every response being received by the client.