Respond with multi line body

Caddy version:

v2.4.1

The problem I’m having:

I would like to create a respond with a multi line body. According to the documentation this is possible: respond (Caddyfile directive) — Caddy Documentation

body is an alternate way to provide a body; convenient if it is multiple lines.

Sadly it is not documented how I have to enter the multi line body.

What I already tried:

I have already try this:

respond /robots.txt {
    body "User-agent: * 
             Disallow: /"
}

But how I expected this is not a valid Caddyfile:

run: adapting config using caddyfile: parsing caddyfile tokens for 'respond': /etc/caddy/Caddyfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'respond'

How can I enter the newline?

The one line version does work:

respond /robots.txt "User-agent: * Disallow: /"

You can use backticks ` for multi-line strings. It’s explained in the Tokens and Quotes section:

Double quotes should work too. I don’t think the multi-line body is the problem. There might be a discrepancy between the docs and the parsing logic. (Not sure if bug in docs or parser, though.)

Actually, the problem is you missed the required status code argument:

:80

respond /robots.txt 200 {
    body "User-agent: *
             Disallow: /"
}

:point_down:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "match": [
                {
                  "path": [
                    "/robots.txt"
                  ]
                }
              ],
              "handle": [
                {
                  "body": "User-agent: * \n             Disallow: /",
                  "handler": "static_response",
                  "status_code": 200
                }
              ]
            }
          ]
        }
      }
    }
  }
}

Unfortunately, there’s no way to avoid the leading spaces on the subsequent lines without aligning it to the left gutter, like this:

:80

respond /robots.txt 200 {
    body "User-agent: *
Disallow: /"
}

I proposed a solution by implementing heredoc support in the Caddyfile, but it was rejected for having a weird implementation :upside_down_face:

2 Likes

Ah yep, that’s what I was wondering. I have to admit I had to read the docs twice but I think both the docs and the parser are in harmony.

Thanks for the help. I did misunderstand the documentation and had though that the status code is optimal.

simple " with status codes does work:

respond /robots.txt 200 {
	        body "User-agent: *
Disallow: /"
}

But the same with additional ` does not work.

        respond /robots.txt 200 {
                body `"User-agent: *
Disallow: /"`

If I use this, the " are included in the responds:

"User-agent: *
Disallow: /"
1 Like

Backticks are a replacement for double quotes, mainly for when you actually do want double quotes in the output. Most typical example is when you need to emit JSON. For example:

respond `{"foo": "bar"}`

instead of:

respond "{\"foo\": \"bar\"}"

Both work, but clearly the first is better because it avoids the mess of escaping.

1 Like