使用 cpr C++ 请求库发送 nlohmann json 有效负载的最佳方法

Ole*_*siy 4 c++

我正在尝试使用两个 C++ 库将 JSON 对象发布到外部服务器:nlohmann/jsonwhoshuu/cpr

在 python 中我可以简单地通过使用来做到这一点requests.post(url, json=data)

有没有一种简单的方法将nlohmann::json类转换为cpr::Payload所需的等效项cpr::POST

Kur*_*man 6

你想要像他们的文档中的这个例子一样:

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::Bodydump()方法。