How to Return JSON response using Caddy File Server

June 28th 2020/Share/Edit

This is a very quick post quickly outlining how to get Caddy to respond with JSON when making requests to the file server that it can host.

I spent a day scouring the corners of the internet to find if it supported such functionality, and flipped flopped back and forth as to whether I would have to build my own API to simply index the directory and serve the files myself.

Luckily! Caddy does support this functionality and it's as simple as changing the accept header in your requests that you make to route hosted with Caddy.

Changing Accept Header in Javascript

fetch("", {
  headers: {
    accept: "application/json",
}).then(res => console.log(res.body.toJson()))

Changing Accept Header in Postman

Below you can see how it's done in Postman:

Postman accept header settings

It's important to note that you need to create another "Accept header" field inside postman in order for the default one to be overwritten. (This is what caught me out originally)