Unable to append to servers via API without overwiting

1. The problem I’m having:

Unable to append to servers through API POST. A POST to /config/apps/http/servers/… simply overwrites any previous servers

2. Error messages and/or full log output:

3. Caddy version:

2.7.5

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04.3 LTS , installed through apt

b. Command:

Sending a POST to /config/apps/http/servers/… with the following body

{
    "bye": {
        "listen": [
            ":2016"
        ],
        "routes": [
            {
                "handle": [
                    {
                        "handler": "static_response",
                        "body": "Goodbye, world!"
                    }
                ]
            }
        ]
    }
}

c. Service/unit/compose file:

d. My complete Caddy config:

{   
    "apps": {
        "http": {
            "http_port": 8080,
            "https_port": 4343,
            "servers": {
                 "hello": {
                    "listen": [
                        ":2015"
                    ],
                    "routes": [
                    {
                        "handle": [
                        {
                            "handler": "static_response",
                            "body": "Hello, world!"
                        }
                        ]
                    }
                    ]
                }
            }
        }
    }
}   


5. Links to relevant resources:

Having the same problem as the person here : Unable to append http/servers config through API

If I try to make the servers block an array I see this error as a response when trying to append to servers :

Error: loading initial config: loading new config: loading http app module: decoding module config: http: json: cannot unmarshal array into Go struct field App.servers of type map[string]*caddyhttp.Server

/apps/http/servers/ is a map (object), not an array:

hence POST replaces the value.

Right. So my objective is to have a dynamically populated list of servers updated via the API.

What approach would you suggest to accomplish this without rewriting the entire list of servers every time a change to 1 server’s options needs to happen?

I did it like this:

$ curl -H "Content-Type: application/json" \
    -d '{ <...> }' \
     "http://localhost:2019/config/apps/http/servers/<name>"

Replace <...> with the server config and <name> with the name you want to give the server (the name isn’t important, it’s for your own reference and convenience).

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