How to check Content-type?

Hello! I need to check request header (content-type) if it “application/json” to do reverse proxy if not respond 404. I need users who use the browser at /api/* to see the 404 error, now they see my API :frowning:

My current caddyfile:

example.com
log
encode gzip
root * /usr/share/caddy
handle /api/* {
	reverse_proxy django:8000
}
handle /admin/* {
	reverse_proxy django:8000
}
handle {
	try_files {path} /index.html
	file_server browse
}

I need something like this:

example.com
log
encode gzip
root * /usr/share/caddy
@contenttype {
	header Content-Type application/json
}
handle @contenttype {
  respond "Access denied" 404 {
     close
  }
}
handle /api/* {
	reverse_proxy django:8000
}
handle /admin/* {
	reverse_proxy django:8000
}
handle {
	try_files {path} /index.html
	file_server browse
}

Backend: Django + API + caddy + SPA(frontend).

I think you’re missing the not matcher:

@contenttype {
	not header Content-Type application/json
}

Probably something more like this:

@notJson {
	not header Content-Type application/json
}
handle /api/* {
	respond @notJson 404
	reverse_proxy django:8000
}
2 Likes

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