Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response.Body() cant‘run in the page.OnResponse(HandleResponse) callback function #391

Open
xiaoyaochen opened this issue Nov 22, 2023 · 3 comments

Comments

@xiaoyaochen
Copy link

xiaoyaochen commented Nov 22, 2023

when use response.Body() in the page.OnResponse(HandleResponse) callback function, it will be stop running
like this:

package main

import (
	"fmt"

	"github.com/playwright-community/playwright-go"
)

func HandleResponse(response playwright.Response) {
	fmt.Println(response.URL())
	body, err := response.Body()
	if err == nil {
		fmt.Println(body)
	}
}

func main() {
	pw, _ := playwright.Run()
	browser, _ := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Headless: playwright.Bool(true),
	})
	context, _ := browser.NewContext()
	page, _ := context.NewPage()
	page.OnResponse(HandleResponse)
	_, err := page.Goto("https://github.com/")
	if err != nil {
		fmt.Printf(err.Error())
	}
	defer func() {
		page.Close()
		browser.Close()
		pw.Stop()
	}()
}

@canstand
Copy link
Collaborator

Please provide verified reproducible code in markdown code block format.

@xiaoyaochen
Copy link
Author

package main

import (
	"fmt"

	"github.com/playwright-community/playwright-go"
)

func HandleResponse(response playwright.Response) {
	fmt.Println(response.URL())
	body, err := response.Body()
	if err == nil {
		fmt.Println(body)
	}
}

func main() {
	pw, _ := playwright.Run()
	browser, _ := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Headless: playwright.Bool(true),
	})
	context, _ := browser.NewContext()
	page, _ := context.NewPage()
	page.OnResponse(HandleResponse)
	_, err := page.Goto("https://github.com/")
	if err != nil {
		fmt.Printf(err.Error())
	}
	defer func() {
		page.Close()
		browser.Close()
		pw.Stop()
	}()
}

@canstand
Copy link
Collaborator

pls use goroutine to get response body for now:

func HandleResponse(response playwright.Response) {
	go func() {
		fmt.Println(response.URL())
		body, err := response.Body()
		if err == nil {
			fmt.Println(body)
		}
	}()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants