DocsHTTP ServerReading params

Reading params

Pull path parameters, wildcards, and the matched route pattern without importing chi.

A route pattern names its dynamic segments, and a handler reads them back through three helpers. They wrap chi so a handler never imports it, which keeps the router the only place that knows the underlying matcher.

Path parameters

A {name} placeholder in a pattern is read with Param. It returns the matched value, or an empty string when the route has no such parameter.

r.Get("/items/{id}", func(w http.ResponseWriter, r *http.Request) {
	id := server.Param(r, "id")
	server.WriteJSON(w, http.StatusOK, map[string]string{"id": id})
})

Wildcards

A trailing /* in a pattern matches the rest of the path. Read everything it captured with Wildcard.

r.Get("/files/*", func(w http.ResponseWriter, r *http.Request) {
	path := server.Wildcard(r) // everything after /files/
	serveFile(w, path)
})

The matched pattern

RoutePattern returns the pattern that matched the request, such as /items/{id}, rather than the concrete path. Use it as a low-cardinality label for metrics and structured logging, where the raw path would explode the key space.

logger.Info("request", "route", server.RoutePattern(r), "method", r.Method)

It returns an empty string when called outside a matched route, so guard on that if a middleware might run before matching.