Remove % from plain-text response

1. The problem I’m having:

I’ve added this subdomain block in my Caddyfile:

ip.mydomain.com {
	templates
	header Content-Type text/plain
	respond "{{.RemoteIP}}"
	encode zstd gzip
}

Whe I perform a curl to it from a terminal (macOS), I get the IP address, as expected, but a % symbol at the end as well.

What’s causing this and how can I remove it, please?

curl https://ip.mydomain.com
45.155.43.53%

Also, is it possible to receive the same response regardlress of whether I’m using https or http?

Thanks!

2. Error messages and/or full log output:

N/A

3. Caddy version:

v2.10.0 h1:fonubSaQKF1YANl8TXqGcn4IbIRUDdfAkpcsfI/vX5U=

4. How I installed and ran Caddy:

a. System environment:

Docker

That’s from the shell, not Caddy. The shell is telling you the response comes without \n.

Yes

1 Like

I thought as much, but here’s what I get if I add \n at the end – respond "{{.RemoteIP}}\n":

curl https://ip.mydomain.com
10.0.0.1\n%

Would you mind telling how to get a response for http, as well as https. please?

Check what you are getting with curl -v - although it appears blank without -v you are probably getting a redirect to https.

1 Like

caddy doesnt unescape string “\n” to new line apparently. but this works

:80 {
	templates
	header Content-Type text/plain
	respond "{{.RemoteIP}}
"
}

Thank you! I did that and now I’m getting just the IP address. The important thing was to put the closing quote at the beginning of the line, with no indentation.

You’re right, there’s a redirect:

* Request completely sent off
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Tue, 23 Sep 2025 06:25:51 GMT
< Content-Type: text/html
< Content-Length: 162
< Connection: keep-alive
< Location: https://ip.mydomain.com/

What do I have to do in order to get the IP, instead of the redirect?

Also, {{.RemoteIP}}, when queried from within the local network, returns the gateway’s IP – 10.0.0.1. How can I get the external address of the requesting machine?

You can disable the automatic redirect: Global options (Caddyfile) — Caddy Documentation

That would be your gateway doing NAT I think, unless it is also a reverse proxy. In that case, you can’t.

1 Like

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