#include #include #include #include #include #include #include #define copt curlpp::options using json = nlohmann::json; std::string getNames() { json j; j["addr"] = nullptr; j["port"] = 8887; json t; t["port"] = "/dev/cu.Bluetooth-Incoming-Port"; t["bandwidth"] = 115200; j["arduino"] = t; return j.dump(); } std::string dump_state() { json j; j["pi"] = 3.14159265359; j["hello"] = "World"; std::stringstream s; for (int i=0; i<3; i++) { s.str(""); s << "loore " << i; j["manage"][i] = (std::string) s.str(); j["vote"][s.str()] = i; } return j.dump(); } std::string get(const std::string url) { curlpp::Cleanup myCleanup; std::ostringstream os; os << curlpp::options::Url(std::string(url)); return os.str(); } std::string post(std::string url, std::string json) { std::list header; header.push_back("Content-Type: application/json"); curlpp::Cleanup clean; curlpp::Easy re; re.setOpt(new copt::Url(url)); re.setOpt(new copt::HttpHeader(header)); re.setOpt(new copt::PostFields(json)); re.setOpt(new copt::PostFieldSize(json.length())); std::ostringstream os; re.setOpt(new curlpp::options::WriteStream(&os)); re.perform(); return std::string(os.str()); } void names() { std::vector names; std::string name; while (1) { std::cout << "What is your name? "; getline(std::cin, name); if (name.length()) { names.push_back(name); } else { break; } } for (std::string name : names) { std::cout << "Hello, " << name << "!" << std::endl; } } int main() { std::string m = getNames(); if (m.length()) { std::cout << m << std::endl; return 0; } // std::string ts = get("https://raw.githubusercontent.com/nlohmann/json/develop/test/data/json.org/1.json"); std::string s = dump_state(); try { s = post("http://localhost/post2echo.php", s); } catch( const std::exception & ex ) { std::cerr << ex.what() << std::endl; }; std::cout << s << std::endl; return 0; }