ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
brew install nlohmann_json
brew install curlpp
- CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(netq)
set(CMAKE_CXX_STANDARD 17)
# find_package(nlohmann_json 3.2.0 REQUIRED)
link_directories(/usr/local/Cellar/curlpp/0.8.1)
link_directories(/usr/local/Cellar/nlohmann-json/3.7.0)
find_package(nlohmann_json REQUIRED PATHS /usr/local/Cellar/nlohmann-json/3.7.0/ NO_DEFAULT_PATH)
add_executable(netq main.cpp)
target_link_libraries(netq LINK_PUBLIC curl curlpp nlohmann_json::nlohmann_json)
# target_link_libraries(netq PRIVATE nlohmann_json::nlohmann_json)
- main.cpp
#include <iostream>
#include <sstream>
#include <math.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <nlohmann/json.hpp>
#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<std::string> 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<std::string> 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;
}