Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting and receiving JSON payload with curlpp

With the curlpp C++ wrapper for libcurl how do I specify JSON payload for a post request and how can I receive JSON payload in response? Where do I go from here:

std::string json("{}");

std::list<std::string> header;
header.push_back("Content-Type: application/json");

cURLpp::Easy r;
r.setOpt(new curlpp::options::Url(url));
r.setOpt(new curlpp::options::HttpHeader(header));
// set payload from json?
r.perform();

Then, how do I await for a (JSON) response and retrieve the body?

like image 244
Oleg Sklyar Avatar asked Jan 31 '17 12:01

Oleg Sklyar


People also ask

How do I post JSON with Curl?

How do I POST JSON with Curl? To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter.

Why can't I send JSON when I send data using cURL?

If you are submitting data using Curl and you do not explicitly specify the content type, Curl uses the application/x-www-form-urlencoded content type for your data. Therefore, when you send JSON (or any other data type), you need to explicitly specify the data type using the -H "Content-Type: application/json" command line parameter.

How is the curl/bash code generated for the JSON payload example?

The Curl/Bash code was automatically generated for the JSON Payload example. Curl/Bash The authorization header will be automatically generated when you send the request. Read more about HTTP Authentication. Curl/Bash

How do I send a POST request with Curl?

There are two ways to send a POST request with Curl. When you use command-line parameters such as --data or --form and do not explicitly specify the required HTTP method, Curl automatically selects the POST method and sends a POST request with the application/x-www-form-urlencoded content type (or multipart/form-data for --form).


2 Answers

Turns out this is fairly straightforward to do, even asynchronously:

std::future<std::string> invoke(std::string const& url, std::string const& body) {
  return std::async(std::launch::async,
    [](std::string const& url, std::string const& body) mutable {
      std::list<std::string> header;
      header.push_back("Content-Type: application/json");

      curlpp::Cleanup clean;
      curlpp::Easy r;
      r.setOpt(new curlpp::options::Url(url));
      r.setOpt(new curlpp::options::HttpHeader(header));
      r.setOpt(new curlpp::options::PostFields(body));
      r.setOpt(new curlpp::options::PostFieldSize(body.length()));

      std::ostringstream response;
      r.setOpt(new curlpp::options::WriteStream(&response));

      r.perform();

      return std::string(response.str());
    }, url, body);
}
like image 175
Oleg Sklyar Avatar answered Sep 19 '22 06:09

Oleg Sklyar


By analyzing the documentation, the fifth example shows how to set a callback to get a response:

// Set the writer callback to enable cURL to write result in a memory area
curlpp::types::WriteFunctionFunctor functor(WriteMemoryCallback);
curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor);
request.setOpt(test);

where the callback is defined as

size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb)

Since a response can arrive in chunks, it can be called multiple times. Once the response is completed, use a JSON library to parse it.

like image 33
karastojko Avatar answered Sep 19 '22 06:09

karastojko