Accessing json api with axios

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

via systemd

a. System environment:

Ubuntu 18.04

b. Command:

none - auto-started

c. Service/unit/compose file:

n/a

d. My complete Caddyfile or JSON config:

{
    "apps":{
       "http":{
          "servers":{
             "srv0":{
                "automatic_https":{
                   "disable":true
                },
                "listen":[
                   ":80"
                ],
                "routes":[
                   {
                      "group":"group1",
                      "handle":[
                         {
                            "handler":"vars",
                            "root":"/opt/ivt/photography"
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/files/*"
                            ]
                         }
                      ]
                   },
                   {
                      "group":"group1",
                      "handle":[
                         {
                            "handler":"vars",
                            "root":"/opt/ivt/apps/6.0.0/packages/client/spa"
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/console_socket",
                                              "replace":"/console/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3001"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/console_socket/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/web_app_socket",
                                              "replace":"/web/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3001"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/web_app_socket/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/liveview",
                                              "replace":"/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3004"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/liveview/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/weather",
                                              "replace":"/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3010"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/weather/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/archive",
                                              "replace":"/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3003"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/archive/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/alarms",
                                              "replace":"/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3002"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/alarms/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"rewrite",
                                        "uri_substring":[
                                           {
                                              "find":"/ptz",
                                              "replace":"/socket.io"
                                           }
                                        ]
                                     }
                                  ]
                               },
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3006"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/ptz/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"subroute",
                            "routes":[
                               {
                                  "handle":[
                                     {
                                        "handler":"reverse_proxy",
                                        "headers":{
                                           "request":{
                                              "set":{
                                                 "Access-Control-Allow-Credentials":[
                                                    "true"
                                                 ],
                                                 "Access-Control-Allow-Headers":[
                                                    "Cache-Control,Content-Type"
                                                 ],
                                                 "Access-Control-Allow-Origin":[
                                                    "*"
                                                 ],
                                                 "X-Forwarded-Proto":[
                                                    "{http.request.scheme}"
                                                 ],
                                                 "X-Real-Ip":[
                                                    "{http.request.remote.host}"
                                                 ]
                                              }
                                           }
                                        },
                                        "upstreams":[
                                           {
                                              "dial":"localhost:3001"
                                           }
                                        ]
                                     }
                                  ]
                               }
                            ]
                         }
                      ],
                      "match":[
                         {
                            "path":[
                               "/api/*"
                            ]
                         }
                      ]
                   },
                   {
                      "handle":[
                         {
                            "handler":"file_server",
                            "hide":[
                               "/etc/caddy/Caddyfile"
                            ]
                         }
                      ]
                   }
                ]
             }
          }
       },
       "tls":{
          "automation":{
             "policies":[
                {
                   "issuer":{
                      "module":"internal"
                   }
                }
             ]
          }
       }
    }
 }

3. The problem I’m having:

I am trying to use json api to load updated config
I am using axios - but I get a 404 error

4. Error messages and/or full log output:

from debug:

Request failed with status code 404
Error: Internal Server Error

from Output:

HTTP/1.1 500 Internal Server Error
Content-Security-Policy: default-src 'none'
X-DNS-Prefetch-Control: off
Expect-CT: max-age=0
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: no-referrer
X-XSS-Protection: 0
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 366
Vary: Accept-Encoding
Date: Tue, 18 Aug 2020 17:34:55 GMT
Connection: keep-alive

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: Internal Server Error<br> &nbsp; &nbsp;at /path/to/file.js:91:22<br> &nbsp; &nbsp;at processTicksAndRejections (internal/process/task_queues.js:97:5)</pre>
</body>
</html>

5. What I already tried:

This is the JavaScript code I am calling:

function putConfig (config) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: 'http://localhost:2019/load/',
      headers: {
        'Content-Type': 'application/json'
      },
      data: { ...config }
    })
      .then(results => {
        console.log(results)
        resolve(results.data)
      })
      .catch(error => {
        console.log(error)
        reject(error.message)
      })
  })
}

I’d like to note that getting the config works with this code and returns the above json config:

function getConfig () {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: 'http://localhost:2019/config/'
    })
      .then(results => {
        // console.log(results)
        resolve(results.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

6. Links to relevant resources:

Basically, if I get the config and try to write it back, even without changes, I get the error.

Also, in journal entry, you can see that using the same caddy.json works with curl, so does the GET to get the config, but the post fails, but does not show that in the journal:

Aug 18 11:32:33 FLEX-5 caddy[2010]: {"level":"info","ts":1597771953.6003535,"logger":"admin.api","msg":"received request","method":"POST","host":"localhost:2019","uri":"/load","remote_addr":"127.0.0.1:57316","headers":{"Accept":["*/*"],"Content-Length":["24158"],"Content-Type":["application/json"],"Expect":["100-continue"],"User-Agent":["curl/7.58.0"]}}
Aug 18 11:32:33 FLEX-5 caddy[2010]: {"level":"info","ts":1597771953.6019025,"logger":"admin.api","msg":"config is unchanged"}
Aug 18 11:32:33 FLEX-5 caddy[2010]: {"level":"info","ts":1597771953.60193,"logger":"admin.api","msg":"load complete"}
Aug 18 11:34:03 FLEX-5 caddy[2010]: {"level":"info","ts":1597772043.0425045,"logger":"admin.api","msg":"received request","method":"GET","host":"localhost:2019","uri":"/config/","remote_addr":"127.0.0.1:58058","headers":{"Accept":["application/json, text/plain, */*"],"Connection":["close"],"User-Agent":["axios/0.19.2"]}}
Aug 18 11:34:50 FLEX-5 caddy[2010]: {"level":"info","ts":1597772090.3445845,"logger":"admin.api","msg":"received request","method":"POST","host":"localhost:2019","uri":"/load/","remote_addr":"127.0.0.1:58444","headers":{"Accept":["application/json, text/plain, */*"],"Connection":["close"],"Content-Length":["6722"],"Content-Type":["application/json"],"User-Agent":["axios/0.19.2"]}}

Ok, looking at my own posts, I noticed a difference between CURL and axios path and it was this line:

url: 'http://localhost:2019/load/',

and when removing the trailing’/’ it now works.

2 Likes

I think you want http://localhost:2019/load, note the trailing /.

Edit: :man_facepalming: you figured it out already I commented after reading the top post only, my bad :joy:

2 Likes

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