我正在尝试使用两个 C++ 库将 JSON 对象发布到外部服务器:nlohmann/json和whoshuu/cpr
在 python 中我可以简单地通过使用来做到这一点requests.post(url, json=data)
有没有一种简单的方法将nlohmann::json类转换为cpr::Payload所需的等效项cpr::POST?
你想要像他们的文档中的这个例子一样:
auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
cpr::Body{"This is raw POST data"},
cpr::Header{{"Content-Type", "text/plain"}});
Run Code Online (Sandbox Code Playgroud)
不要使用 using cpr::Payload,而是使用json 对象上的cpr::Body和dump()方法。