Process POST data with Go handler

My main issue is that I am a newbie so I got stuck while implementing. Right now I have this:
The form is served via Caddy (root directory)
I have a Go process running on port 3000 that should handle the data. I have tested this handler with a /template index.html form and works fine. How do I tell the Caddy server that the POST will be handled by the process running on port 3000?

Hi @ETua, welcome to the Caddy community.

It sounds like you want to proxy a request from Caddy to your Go process, but only for POST requests.

The proxy directive can’t distinguish between methods by itself, but you can use the rewrite directive with an if statement to check the method via placeholder. It might look like this:

example.com {
	# Prepend with "/post" so the proxy knows which requests to forward on
	rewrite /endpoint {
		if {method} is POST
		to /post{uri}
	}

	# Proxy anything starting with "/post", but strip out the prefix first
	proxy /post localhost:3000 {
		without /post
		# transparent might be useful if your app needs info on the actual client
	}
}

Changing /post and /endpoint to suit your requirements, of course.

https://caddyserver.com/docs/rewrite
https://caddyserver.com/docs/proxy
https://caddyserver.com/docs/placeholders

1 Like

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