diff --git a/lib_standalone/HTTPClient.h b/lib_standalone/HTTPClient.h new file mode 100644 index 000000000..44b733a01 --- /dev/null +++ b/lib_standalone/HTTPClient.h @@ -0,0 +1,27 @@ +#ifndef HTTPClient_H_ +#define HTTPClient_H_ + +#include "WString.h" + +class HTTPClient { + public: + // HTTPClient(); + // ~HTTPClient(); + + bool begin(String url) { + return true; + }; + void end(void) {}; + int GET() { + return 200; + }; + int POST(String payload) { + return 200; + }; + void addHeader(const String & name, const String & value, bool first = false, bool replace = true) {}; + String getString(void) { + return "Hello, World!"; + }; +}; + +#endif /* HTTPClient_H_ */ diff --git a/lib_standalone/WString.cpp b/lib_standalone/WString.cpp index 994fe9817..fca809c7f 100644 --- a/lib_standalone/WString.cpp +++ b/lib_standalone/WString.cpp @@ -69,4 +69,4 @@ size_t strlcat(char * dst, const char * src, size_t siz) { return (dlen + (s - src)); /* count does not include NUL */ } -#endif \ No newline at end of file +#endif diff --git a/lib_standalone/WString.h b/lib_standalone/WString.h index 04cb3d2e5..ee53d7456 100644 --- a/lib_standalone/WString.h +++ b/lib_standalone/WString.h @@ -53,6 +53,14 @@ class String { return 1; } + int len() const { + return _str.size(); + } + + bool startsWith(const char * prefix) const { + return _str.find(prefix) == 0; + } + private: std::string _str; }; diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 41f3f0d99..2093d37e1 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -342,22 +342,44 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha HTTPClient http; String url = doc["url"]; if (http.begin(url)) { + // It's an HTTP call + + // add any given headers for (JsonPair p : doc["header"].as()) { http.addHeader(p.key().c_str(), p.value().as().c_str()); } - String value = doc["value"] | ""; + String value = doc["value"] | data; // extract value if its in the command, or take the data + String method = doc["method"] | "GET"; // default GET + + // if there is data, force a POST if (value.length()) { + if (value.startsWith("{")) { + http.addHeader("Content-Type", "application/json"); // auto-set to JSON + } httpResult = http.POST(value); - } else if (data && data[0] != '\0') { // post - httpResult = http.POST(String(data)); } else { - httpResult = http.GET(); + // no value, but check if it still a POST request + if (method == "POST") { + httpResult = http.POST(value); + } else { + httpResult = http.GET(); // normal GET + } } + http.end(); + + // check HTTP return code + if (httpResult != 200) { + char error[100]; + snprintf(error, sizeof(error), "Schedule %s: URL command failed with http code %d", name, httpResult); + emsesp::EMSESP::logger().warning(error); + return false; + } + return true; } } - return httpResult > 0; } + JsonDocument doc_input; JsonObject input = doc_input.to(); if (strlen(data)) { // empty data queries a value @@ -562,6 +584,8 @@ void WebSchedulerService::test() { // (14 - 40) * 2.8 + 5 = -67.8 test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5"; command("test", test_cmd.c_str(), compute(test_value).c_str()); + + // TODO add some HTTP/URI tests } #endif