Intercept request and serve json instead

1. Caddy version (caddy version):

v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=

2. How I run Caddy:

caddy run --watch

a. System environment:

OS: macOS Monterey

b. Command:

caddy run --watch

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
	auto_https off
}

handle /api/v1/some-endpoint/* {
	rewrite * /local-folder/
	file_server {
		browse "core.json"
	}
}

3. The problem I’m having:

Everytime I go to /api/v1/some-endpoint/* I would like caddy to serve a local json file from disk instead. This json file lives in /local-folder/core.json

4. Error messages and/or full log output:

ERROR http.log.error parsing browse template: parsing browse template file: open core.json: no such file or directory

5. What I already tried:

I’m very new to caddy so sorry for the ignorance. Ive tried different options like redir and also changing my folder structure but im sort of a deer in the headlights as I don’t have very much context on how to do this with caddy.

6. Links to relevant resources:

Please upgrade to v2.4.6! There’s an important security fix.

browse is not the solution for that. The browse template is what produces the UI for the “file browser”. Caddy ships with a default template which renders out the requested directory’s contents with HTML, but that template can be replaced and customized to your liking if you want to theme it etc.

If you want to just serve a particular file do a rewrite to that file, then use root + file_server to serve it.

handle /api/vi/some-endpoint/* {
	root * /path/to/local-files
	rewrite /core.json
	file_server
}

This would serve the file found at /path/to/local-files/core.json for all requests matching the path /api/vi/some-endpoint/*

Thank you so much for the response, but im getting an unexpected argument count for the rewrite.

watcher unable to load latest config    {"config_file": "Caddyfile", "error": "adapting config using caddyfile: parsing caddyfile tokens for 'handle': Caddyfile:37 - Error during parsing: parsing caddyfile tokens for 'rewrite': Caddyfile:35 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'"}

Should it instead be?

rewrite * /core.json

Actually, even that seems to throw a 404…

Yeah, that’s right. My bad. Since /core.json starts with a /, the Caddyfile parser considers that first argument to be a path matcher, and not as the actual rewrite path argument.

Does the caddy process have permissions to read that file?

Turn on the debug global option, the logs will have more details about what file_server is doing.

I was able to get it working. Thanks so much for your help!

The result ending up needing to be:
Note: No leading / in front of path/to/local-files

handle /api/vi/some-endpoint/* {
	root * path/to/local-files
	rewrite * /core.json
	file_server
}

The leading slash means that the path is absolute. If you remove the leading slash, it’s a relative path, i.e. relative from the working directly Caddy was run from.

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