-
-
Notifications
You must be signed in to change notification settings - Fork 424
Description
Rod Version: v0.116.2
Hi, I'm trying to read body of response which has 302 status code, but can't do it using events (I know about rod.Hijack, but it's not the same because in this case requests are sent using go client) I want to solve this using only event handlers, is it possible?
The code to demonstrate question
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
)
func main() {
go startServer()
launcher := launcher.New().Bin("/opt/google/chrome/chrome")
controlURL, err := launcher.Launch()
if err != nil {
log.Fatalf("Failed to launch browser: %v", err)
}
browser := rod.New().ControlURL(controlURL).MustConnect()
defer browser.Close()
page := browser.MustPage()
err = proto.NetworkEnable{}.Call(page)
if err != nil {
log.Fatalf("Failed to enable network domain: %v", err)
}
done := make(chan struct{})
go page.EachEvent(func(e *proto.NetworkResponseReceived) {
if e.Response.Status >= 300 && e.Response.Status < 400 {
fmt.Printf("Redirect response detected!\n")
fmt.Printf("URL: %s\n", e.Response.URL)
fmt.Printf("Status: %d\n", e.Response.Status)
res, err := proto.NetworkGetResponseBody{RequestID: e.RequestID}.Call(page)
if err != nil {
log.Printf("Could not get response body for redirect: %v\n", err)
} else {
fmt.Printf("Redirect response body:\n---\n%s\n---\n", res.Body)
}
close(done)
}
})()
page.MustNavigate("http://localhost:8080/redirect-me")
select {
case <-done:
fmt.Println("Successfully intercepted redirect and read response body.")
case <-time.After(10 * time.Second):
fmt.Println("Timeout: Did not intercept redirect.")
}
}
func startServer() {
http.HandleFunc("/redirect-me", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "/final-destination")
w.WriteHeader(http.StatusMovedPermanently)
_, _ = w.Write([]byte("Redirecting... I want to read this body"))
return
})
http.HandleFunc("/final-destination", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "You have arrived at your final destination.")
})
log.Println("Starting server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}What you got
NetworkResponseReceived event doesn't fire for responses with 301, 302 status codes, so I can't read body. I know about RedirectResponse field in NetworkRequestWillBeSent but I can't get body in this event too, only path and headers.
What you expect to see
I expect NetworkResponseReceived and NetworkLoadingFinished called for response with redirect too.
What have you tried to solve the question
I tried to see if body available in any other events (NetworkRequestWillBeSentExtraInfo, NetworkResponseReceivedExtraInfo) but this doesn't help.