playwright-community / playwright-go

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
https://playwright-community.github.io/playwright-go/
MIT License
2.11k stars 156 forks source link

About Obtaining All Request Headers in an HTTP Request #398

Closed ctinkong closed 7 months ago

ctinkong commented 9 months ago

I want to obtain all the request headers when making an HTTP request. I've tried using

request.AllHeaders(), request.HeadersArray(), and request.HeaderValue()

methods within

page.OnRequest or page.OnRequestFinished

but I can only retrieve a few headers such as

Referer and User-Agent

headers. I am unable to get other headers, such as

Cookie

I also attempted using

page.Context().Cookies()

but with the same result. So, how can I retrieve all the request headers during an HTTP request? Thank you.

canstand commented 9 months ago

If you just need cookies, page.Context().Cookies() should work. If you need request headers, request.AllHeaders() should work.

Note, do not block request:

    chanReq := make(chan playwright.Request, 1)
    page.OnRequest(func(r playwright.Request) {
        // do not block request
        go func() {
            chanReq <- r
        }()
    })

    // handle request in another goroutine
    go func() {
        req := <-chanReq
        headers, err := req.AllHeaders()
        if err != nil {
            log.Print(err)
        } else {
            log.Print(headers)
        }
    }()
ctinkong commented 9 months ago

OK,I get it.The way I used is problematic.

request.AllHeaders() will block

page.OnRequest(func(r playwright.Request) { // r.AllHeaders() is blocking headers, _ := r.AllHeaders() })

Am I right? Thank you.

如果您只需要 cookie,应该可以工作。如果您需要请求标头,应该可以工作。page.Context().Cookies()``request.AllHeaders()

请注意,不要阻止请求:

  chanReq := make(chan playwright.Request, 1)
  page.OnRequest(func(r playwright.Request) {
      // do not block request
      go func() {
          chanReq <- r
      }()
  })

  // handle request in another goroutine
  go func() {
      req := <-chanReq
      headers, err := req.AllHeaders()
      if err != nil {
          log.Print(err)
      } else {
          log.Print(headers)
      }
  }()

If you just need cookies, page.Context().Cookies() should work. If you need request headers, request.AllHeaders() should work.

Note, do not block request:

  chanReq := make(chan playwright.Request, 1)
  page.OnRequest(func(r playwright.Request) {
      // do not block request
      go func() {
          chanReq <- r
      }()
  })

  // handle request in another goroutine
  go func() {
      req := <-chanReq
      headers, err := req.AllHeaders()
      if err != nil {
          log.Print(err)
      } else {
          log.Print(headers)
      }
  }()

OK,I get it.The way I used is problematic.

request.AllHeaders() will block

page.OnRequest(func(r playwright.Request) { // r.AllHeaders() is blocking headers, _ := r.AllHeaders() })

Am I right? Thank you.

canstand commented 9 months ago

right, currently all OnSomeEvent may have the same issue (playwrigh-go only). Hope it can be solved later.

ctinkong commented 9 months ago

right, currently all OnSomeEvent may have the same issue (playwrigh-go only). Hope it can be solved later.

I tried, but unfortunately, req.AllHeaders() blocked.

    chanReq := make(chan playwright.Request, 1)
    page.OnRequest(func(r playwright.Request) {
        // do not block request
        go func() {
            chanReq <- r
        }()
    })

    // handle request in another goroutine
    go func() {
        req := <-chanReq
        headers, err := req.AllHeaders() // Here is blocked.
        if err != nil {
            log.Print(err)
        } else {
            log.Print(headers)
        }
    }()
reginaldl commented 8 months ago

I haven't had the time to dig more but when calling AllHeaders in a goroutine there be might a race in ActualHeaders. At least I get a lot of complaints when running with -race.

ctinkong commented 7 months ago

use page.Context().Cookies(url) to fix it. thanks.