har

package
v1.47.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package har provides HAR 1.2 types and an HTTP client middleware for capturing outbound request/response pairs for troubleshooting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(cfg HARConfig, handler func(*Entry)) middlewares.Middleware

NewMiddleware returns a middlewares.Middleware that captures each request/response pair into a *Entry and calls handler. If handler is nil, the middleware is a no-op.

Types

type Cache

type Cache struct{}

Cache holds cache information for an entry (required by spec; left empty by hx).

type Collector

type Collector struct {
	Config HARConfig
	// contains filtered or unexported fields
}

Collector accumulates HAR entries from multiple sources (main requests, OAuth token fetches, redirect hops, retries).

func NewCollector

func NewCollector(cfg HARConfig) *Collector

func (*Collector) Add

func (c *Collector) Add(e *Entry)

Add appends an entry to the collector. Safe for concurrent use.

func (*Collector) Entries

func (c *Collector) Entries() []Entry

Entries returns a copy of all collected entries.

func (*Collector) Handler

func (c *Collector) Handler() func(*Entry)

Handler returns a func(*Entry) that adds entries to this collector. Useful for passing to components that accept a HAR handler callback.

func (*Collector) Middleware

func (c *Collector) Middleware() middlewares.Middleware

Middleware returns a transport middleware that captures each request/response into this collector.

type Content

type Content struct {
	Size      int64  `json:"size"`
	MimeType  string `json:"mimeType,omitempty"`
	Text      string `json:"text,omitempty"`
	Truncated bool   `json:"truncated,omitempty"`
}

Content holds the response body details.

type Cookie struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

Cookie is a name/value pair from a Cookie or Set-Cookie header.

type Creator

type Creator struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

Creator identifies the application that created the HAR log.

type Entry

type Entry struct {
	StartedDateTime string   `json:"startedDateTime"`
	Time            float64  `json:"time"`
	Request         Request  `json:"request"`
	Response        Response `json:"response"`
	Cache           Cache    `json:"cache"`
	Timings         Timings  `json:"timings"`
}

Entry represents a single HTTP request/response pair.

func CaptureRedirect

func CaptureRedirect(req *http.Request, resp *http.Response, cfg HARConfig) *Entry

CaptureRedirect builds a HAR entry from a redirect hop's request and response.

type File

type File struct {
	Log Log `json:"log"`
}

File is the outermost HAR 1.2 envelope: {"log": {...}}. Use this when writing .har files for import into browser DevTools.

type HARConfig

type HARConfig struct {
	// MaxBodySize is the maximum number of bytes captured per body.
	// Bodies exceeding this are truncated and Content.Truncated is set to true.
	// Default: 65536 (64 KB).
	MaxBodySize int64

	// CaptureContentTypes lists MIME type prefixes for which body capture is enabled.
	// Default: ["application/json", "application/x-www-form-urlencoded"].
	CaptureContentTypes []string

	// RedactedHeaders lists additional header name glob patterns to redact,
	// on top of logger.CommonRedactedHeaders.
	RedactedHeaders []string
}

HARConfig controls what the HAR middleware captures and how it redacts.

func DefaultConfig

func DefaultConfig() HARConfig

DefaultConfig returns a HARConfig with sensible defaults.

type Header struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

Header is a name/value pair.

type Log

type Log struct {
	Version string  `json:"version"`
	Creator Creator `json:"creator"`
	Pages   []Page  `json:"pages"`
	Entries []Entry `json:"entries"`
}

Log is the top-level HAR 1.2 container.

type Page

type Page struct {
	StartedDateTime string      `json:"startedDateTime"`
	ID              string      `json:"id"`
	Title           string      `json:"title"`
	PageTimings     PageTimings `json:"pageTimings"`
}

Page is included for HAR 1.2 spec compliance; hx leaves it empty.

type PageTimings

type PageTimings struct {
	OnLoad int `json:"onLoad,omitempty"`
}

PageTimings holds page-level timing data (unused by hx, present for spec compliance).

type PostData

type PostData struct {
	MimeType string `json:"mimeType"`
	Text     string `json:"text"`
}

PostData holds the request body details.

type QueryString

type QueryString struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

QueryString is a name/value pair from the URL query string.

type Request

type Request struct {
	Method      string        `json:"method"`
	URL         string        `json:"url"`
	HTTPVersion string        `json:"httpVersion"`
	Cookies     []Cookie      `json:"cookies"`
	Headers     []Header      `json:"headers"`
	QueryString []QueryString `json:"queryString"`
	PostData    *PostData     `json:"postData,omitempty"`
	HeadersSize int           `json:"headersSize"`
	BodySize    int64         `json:"bodySize"`
}

Request holds HAR request data.

type Response

type Response struct {
	Status      int      `json:"status"`
	StatusText  string   `json:"statusText"`
	HTTPVersion string   `json:"httpVersion"`
	Cookies     []Cookie `json:"cookies"`
	Headers     []Header `json:"headers"`
	Content     Content  `json:"content"`
	RedirectURL string   `json:"redirectURL"`
	HeadersSize int      `json:"headersSize"`
	BodySize    int64    `json:"bodySize"`
}

Response holds HAR response data.

type Timings

type Timings struct {
	Send    float64 `json:"send"`
	Wait    float64 `json:"wait"`
	Receive float64 `json:"receive"`
}

Timings records durations (in milliseconds) for the request lifecycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL