Unable to append http/servers config through API

1. Output of caddy version:

I am unsure, I did not set-up Caddy, our web hosting company set-up and is managing the running of Caddy for us. The only method I have to interface with Caddy is through the API.

2. How I run Caddy:

I am unsure

a. System environment:

I know it is a Linux server but I don’t know beyond that. I could find out this and the version if necessary.

d. My complete Caddy config:

This config is changing fairly regularily as I am currently in the messing about stage.

{
    "admin": {
        "listen": "lb.madeopen.co.uk:2019"
    },
    "apps": {
        "http": {
            "servers": {
                "test_srv": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "handle": [
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "reverse_proxy",
                                                    "transport": {
                                                        "protocol": "http",
                                                        "tls": {}
                                                    },
                                                    "upstreams": [
                                                        {
                                                            "dial": "test.yourplatforms.org:443"
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "match": [
                                {},
                                {
                                    "host": [
                                        "lb.madeopen.co.uk"
                                    ]
                                }
                            ],
                            "terminal": true
                        }
                    ]
                }
            }
        }
    }
}

3. The problem I’m having:

I am trying to add a new server to the list of servers through the API. Whatever I do it is replacing the current server config not appending, I am using /… for the config url but with or without that it just replaces the current server config

curl --location --request POST 'http://lb.madeopen.co.uk:2019/config/apps/http/servers/...' \
--header 'Content-Type: application/json' \
--data-raw '{"test_srv":{"listen":[":443"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","transport":{"protocol":"http","tls":{}},"upstreams":[{"dial":"test.yourplatforms.org:443"}]}]}]}],"match":[{},{"host":["lb.madeopen.co.uk"]}],"terminal":true}]}}'

The other server config I plan to set-up is:

{"test_new_srv":{"listen":[":443"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","transport":{"protocol":"http","tls":{}},"upstreams":[{"dial":"test3.yourplatforms.org:443"}]}]}]}],"match":[{},{"host":["lb.madeopen.co.uk"]}],"terminal":true}]}}

It is worth pointing out that none of the yourplatforms.org sub-domains are setup at the moment, I am literally just trying to append a http server config through the API.

My ultimate end goal in case it is useful is to use Caddy as a reverse proxy to a whole bunch of different client domains and sub-domains. Our clients will be able to set-up domains themselves and then create “platforms” within their account (on our main software) that will use a sub domain. As clients add domains and sub-domains our application will use a DNS API to get the records added and then utilise the Caddy API to add the appropriate records for it all to work. I was planning on using a server per client domain.

4. Error messages and/or full log output:

I am not receiving an error

5. What I already tried:

I did try to be a bit clever and just pass a JSON array of two server configs:

curl --location --request POST 'http://lb.madeopen.co.uk:2019/config/apps/http/servers/' \
--header 'Content-Type: application/json' \
--data-raw '[{"test_new_srv":{"listen":[":443"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","transport":{"protocol":"http","tls":{}},"upstreams":[{"dial":"test3.yourplatforms.org:443"}]}]}]}],"match":[{},{"host":["lb.madeopen.co.uk"]}],"terminal":true}]}},
{"test_srv":{"listen":[":443"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","transport":{"protocol":"http","tls":{}},"upstreams":[{"dial":"test.yourplatforms.org:443"}]}]}]}],"match":[{},{"host":["lb.madeopen.co.uk"]}],"terminal":true}]}}
]'

Which results in the following error and I suspect I am using the API incorrectly here:

{
    "error": "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"
}

6. Links to relevant resources:

I’d highly recommend using a Caddyfile first.
Even if your hosting provider only gives you access to the admin API, you can still use a Caddyfile as shown in API — Caddy Documentation

curl "http://lb.madeopen.co.uk:2019/load" \
	-H "Content-Type: text/caddyfile" \
	--data-binary @Caddyfile

using a file called Caddyfile in the folder you are executing curl.
You can still use JSON later down the road.

Furthermore, you can use caddy adapt to convert your Caddyfile to JSON locally.

If you insist, then I will unwrap what’s wrong with your current API attempts.

But just so we are on the same page, you did read API — Caddy Documentation and API Tutorial — Caddy Documentation, right? :innocent:

1 Like

Thankyou for responding, using a Caddyfile initially is actually a really good idea, I will try that and then see how I get on from there.

Yes I have read through those API pages and did the tutorial.

Thank you

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