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

Best way to send nlohmann json payload using cpr C++ Request library

本文关键字:方法 最佳 C++ 请求 负载 发送 CPR nlohmann json 有效 使用      更新时间:2023-10-16

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

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

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

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

auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Body{"This is raw POST data"},
                   cpr::Header{{"Content-Type", "text/plain"}});

不要使用 cpr::Payload ,而是在 json 对象上使用 cpr::Bodydump() 方法。