Using captured groups from path_regexp

1. The problem I’m having:

I’m trying to match paths by regexp and respond with data from its captured groups.

While other placeholders work, those for captured groups just output {http.regexp…}.

2. Error messages and/or full log output:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Server: Caddy
X-Numbers-Header: You requested the number {http.regexp.numbers.1} from darwin
Date: Mon, 22 Apr 2024 23:20:27 GMT
Content-Length: 60

You requested the number {http.regexp.numbers.1} from darwin

3. Caddy version:

v2.7.6

4. How I installed and ran Caddy:

a. System environment:

Via Homebrew on macOS Sonoma.

b. Command:

caddy run

c. Service/unit/compose file:

N/A

d. My complete Caddy config:


:6789

@numbers path_regexp /numbers/(\d+)

route @numbers {
	header {
		X-Numbers-Header "You requested the number {re.numbers.1} from {system.os}"
	}

	respond "You requested the number {re.numbers.1} from {system.os}"
}

5. Links to relevant resources:

:6789

@numbers path_regexp numbers /numbers/(\d+)

route @numbers {
	header {
		X-Numbers-Header "You requested the number {re.numbers.1} from {system.os}"
	}

	respond "You requested the number {re.numbers.1} from {system.os}"
}

Figured it out. See above. I had to add a numbers in @numbers to name my regexp (as the latter is the name for just the matcher, I think).

I guess I missed the part in the docs saying that. I wish there was a working example of this in there.

Actually, in v2.8.0 it will work without specifying the name for the path_regexp matcher, it will inherit the name from the named matcher. I’m working on the docs for that right now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.