The other day, I was working on some "worker" tasks that needed to communicate with a main server via HTTP.

I didn’t want to bother setting up a local server, so I resorted to saving the server responses as local files. I couldn’t recall the library or tool used in that programming language to mock HTTP responses, but then it dawned on me that Caddy is installed on my computer since we utilize it as a webserver for web UIs.

So, why not use Caddy instead?

1. Example

In my case, I wanted to serve the JSON file if certain query parameters were present and if the HTTP verb was POST. If the HTTP verb was PUT, I wanted to serve a 204 No Content response.

Turns out it’s quite easy in Caddyfile:

:8080 {
  root * .

  @backend {
    method POST
    query taskId=1234
  }
  handle @backend {
    method GET
    rewrite * /myfile.json
    file_server
  }

  @backendPut {
    method PUT
  }
  handle @backendPut {
    respond 204
  }
}

caddy run, and voilà!