Simple echo server

1. The problem I’m having:

I am trying to build a simple echo server with the template functionality

2. Error messages and/or full log output:

i’m trying to evaluate a template with a POST Method but get

 HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD

3. Caddy version:

v2.6.4

caddyfile

:80 {
	root * /srv
	file_server
	try_files {path}.html {path}
	templates
	encode zstd gzip
	rewrite /* index.html
}

index.html

{{.RespHeader.Add "Set-Cookie" "myID=test"}}
<!DOCTYPE html>
<html>

<head>
    <title>test</title>

    <style>
        table {
            border-collapse: collapse;

        }

        table,
        th,
        td {
            border-bottom: 1px solid #ddd;
            padding: 15px;
            font-family: monospace;
            font-size: 18px;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        table,
        th {
            text-align: left;
        }

        th {
            background-color: #04AA6D;
            color: white;
        }

        tr:hover {
            background-color: coral;
        }

        .c1 {
            width: 100px;
        }

        .c2 {
            width: 400px;
        }


        td {
            border: 1px solid blue;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    </style>
</head>

<body>

    <main>

        <div class="article-container">

            Request:

            <div style="overflow-x:auto;">
                <table>
                    <tr>
                        <th class="c1">What</th>
                        <th class="c2">Value</th>
                    </tr>

                    <tr>
                        <td class="c1">time</td>
                        <td class="c2">{{placeholder "time.now"}}</td>
                    </tr>
                    <tr>
                        <td class="c1">Method</td>
                        <td class="c2">{{.Req.Method}}</td>
                    </tr>

                    <tr>
                        <td class="c1">URL</td>
                        <td class="c2">{{.Req.URL}}</td>
                    </tr>

                    <tr>
                        <td class="c1">YOUR IP</td>
                        <td class="c2">{{.RemoteIP}}</td>
                    </tr>



                    <tr>
                        <td class="c1">body</td>
                        <td class="c2">{{placeholder "http.request.body"}}</td>
                    </tr>

                    <!-- see https://caddyserver.com/docs/json/apps/http/#docs -->
                    <tr>
                        <td class="c1">query</td>
                        <td class="c2">{{placeholder "http.request.query"}}</td>
                    </tr>

                    <tr>
                        <td class="c1">vars</td>
                        <td class="c2">{{placeholder "http.vars.*"}}</td>
                    </tr>


                </table>
            </div>



            HEADERS:

            <div style="overflow-x:auto;">
                <table>
                    <tr>
                        <th class="c1">Header</th>
                        <th class="c2">Value</th>
                    </tr>
                    {{range $key, $val := .Req.Header}}
                    <tr>
                        <td class="c1">{{$key}}</td>
                        <td class="c2">{{index $val 0}}</td>
                    </tr>
                    {{end}}

                </table>
            </div>

            {{ $cookie := index .Req.Header "Cookie" }}
            {{ $cs := index $cookie 0}}
            {{ $ckv := splitList " " $cs}}

            <div style="overflow-x:auto;">
                <table>
                    <tr>
                        <th class="c1">Cookies</th>

                    </tr>

                    {{range $val := $ckv}}
                    <tr>
                        <td class="c1">{{$val}}</td>

                    </tr>
                    {{end}}

                </table>
            </div>


        </div>
        <div class="sidebar"></div>
    </main>

</body>

</html>%

i understand POST requests are blocked in the file handler but is there another way?

Can you provide a simple example of your Caddyfile?

1 Like

Please completely fill out the help topic template, as per the forum rules. We can’t help without more detail.

2 Likes

I believe this is what we need more details on. It could mean a lot of things but we need more specifics here.

i added it.
didnt add it because i’m not sure how far its relevant because from what i read POST requests are blocked for file_server directive?!

Oh; yeah, we figured (and I think there was an issue raising this originally) it didn’t make sense to accept POST/PUT/PATCH/DELETE for a static file server that doesn’t mutate the files.

I agree it’s unfortunate that GET doesn’t allow payloads, but the upcoming QUERY method should solve that. We can allow QUERY I guess.

wouldnt it then make sense to have a directive “template” that executes only templates that allows it or even “scripts”, e.g. goby lang?

I’m not sure I follow; what do you mean, exactly?

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