request-id

command
v14.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Request IDs: one identifier per request, in the response header and the logs.

requestid.New() installs the middleware. The default generator reuses a client-supplied X-Request-Id header and only mints a UUIDv4 when it is absent - convenient behind a trusted proxy, unsafe when arbitrary clients can choose the value that lands in your logs. requestid.ServerOnlyGenerator is the hardened variant: it ignores the inbound header and always generates the ID server-side.

Read the ID with requestid.Get(ctx) (a shortcut for ctx.ID().(string)).

Learn: install requestid, read the ID with requestid.Get, and pick ServerOnlyGenerator when clients must not choose the value. Before: ../rate-limit

Run:

go run main.go

Try:

curl -i http://localhost:8080/trusted                                        # 200, a generated X-Request-Id
curl -i -H "X-Request-Id: chosen-by-client" http://localhost:8080/trusted    # 200, echoes chosen-by-client
curl -i -H "X-Request-Id: chosen-by-client" http://localhost:8080/hardened   # 200, ignores it and generates its own

Next: ../accesslog

Jump to

Keyboard shortcuts

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