Rewrite POST requests to static files?

I was thinking about using Caddy as a local demo server for developing small client-heavy web apps. These are apps that make a handful of GET and POST calls to various backends. I’d like to stub these calls and have them all return static content. This way the apps can be demoed without requiring all the backend infrastructure.

Caddy seemed like a nice lightweight tool that I hoped would work pretty much out of the box, even if it seems to be not quite Caddy’s intended purpose.

I got everything working except for stubbing POST requests. I tried this:

rewrite {
    regexp ^/proxy/backend_service/run$
    to /demo/fixtures/result.json
}

But I get 405 Method Not Allowed when I hit /proxy/backend_service/run with a POST request. I also tried using the proxy feature, with the same result.

Is there any way around this or should I be looking at a different tool?

Hi @d8amx, welcome to the Caddy community!

You’re probably not going to like this answer, but Caddy’s staticfiles handler doesn’t allow you to POST to it, because it’s not designed to accept content, it just serves files.

You could proxy to another server to handle POST requests, but proxying to itself won’t work (because the request will eventually end up at the staticfiles handler, which will reject the POST).

Maybe CGI might be a workaround? You could write a script that just echoes the result you’re after. Bit hacky, but I don’t think the CGI plugin cares about the method like staticfiles does.

Thanks for the answer. That’s alright, I just wondered if there was something I was missing. In the meantime I got something working using Express.js. Not as elegant as a tiny Caddyfile would have been but it works :slight_smile:

1 Like

This is very easy to do in Caddy 2. Would you like to give it a try? We need more people to use it.

Hi Matt, I’d be curious to check it out, but now that I look at the licensing I think I’ll stick to Express.js for the time being.

What’s wrong with the licensing?

1 Like

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