Skip to content

Network Interception

bonk intercepts network requests and responses using the CDP Fetch domain. You can observe, modify, block, or mock any request.

Intercept Requests

unsub := page.OnRequest(func(r *bonk.Request) {
    fmt.Println(r.Method, r.URL)
    r.Continue()
})
defer unsub()

Warning

You must call r.Continue(), r.Abort(), or r.Fulfill() on every intercepted request. If the handler returns without calling any of these, Continue() is called automatically.

Request Properties

r.URL          // string
r.Method       // string (GET, POST, etc.)
r.Headers      // map[string]string
r.PostData     // string
r.ResourceType // string (Document, Script, Image, etc.)

Modify Headers

page.OnRequest(func(r *bonk.Request) {
    r.SetHeader("Authorization", "Bearer token123")
    r.SetHeader("X-Custom", "value")
    r.Continue()
})

Block Requests

page.OnRequest(func(r *bonk.Request) {
    if strings.Contains(r.URL, "analytics") {
        r.Abort()
        return
    }
    r.Continue()
})

Mock Responses

Respond to a request with custom data:

page.OnRequest(func(r *bonk.Request) {
    if strings.Contains(r.URL, "/api/user") {
        r.Fulfill(200, map[string]string{
            "Content-Type": "application/json",
        }, `{"name": "test"}`)
        return
    }
    r.Continue()
})

Intercept Responses

page.OnResponse(func(r *bonk.Response) {
    fmt.Printf("%d %s\n", r.Status, r.URL)

    body, err := r.Body()
    if err == nil {
        fmt.Println(len(body), "bytes")
    }

    r.Continue()
})

Response Properties

r.URL     // string
r.Status  // int64
r.Headers // map[string]string

Unsubscribe

All handler registration methods return an unsubscribe function:

unsub := page.OnRequest(handler)
// later:
unsub()