Oleksiy
Oleksiy

Reputation: 6567

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

I'm trying to post JSON objects to the external server using two C++ libraries: nlohmann/json and whoshuu/cpr

In python I can do this simply by using requests.post(url, json=data)

Is there a simple way to convert nlohmann::json class to cpr::Payload equivalent needed for cpr::POST?

Upvotes: 5

Views: 3514

Answers (1)

Kurt Stutsman
Kurt Stutsman

Reputation: 4034

You want to do like this example from their documentation:

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

Instead of using cpr::Payload, use cpr::Body and the dump() method on the json object.

Upvotes: 6

Related Questions