caddyhttp: Set Content-Type for static response #4999
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since the
Body
field is astring
, and it is generally configured via command-line or text config file, it follows that a reasonable default Content-Type istext/plain
. This allowstemplates
to render templates included in the response, and makes it clear to clients that they do not need to download a binary file, without extra configuration of this header.We also detect valid JSON (JSON can be a number or quoted string which is easily ambiguous as to Content-Type, but the most common form of JSON is an object or array, so we only do it for those structures) and set
application/json
. But those are the only 2 special cases where we set the header automatically. I think those are probably the most common use cases forrespond
: plain text and hard-coded JSON.This should make using the
static_response
handler less cumbersome and more convenient, especially now that it has a CLI shortcut withcaddy respond
.