Adopting cURLpp

So.. I was using system() to perform CURL requests in my application. There were a few downsides to this:-

  1. Error handling of response.
  2. Messy codes.

My boss talked about using the cURLpp library to replace the existing system() curl mechanism. So this week I tried my hand on it in my Wt Application.

Examples

There are plenty of cURLpp examples available in the git repo. However, the documentation is quite lacking. For some usage that is not shown in the examples, you most certainly need to rely on your own resourcefulness.

Application

cURLpp is a C++ wrapper for libCurl, so both libraries must be installed before it can be used. Note that the project must be linked against curl and curlpp. I did a speed comparison between cURLpp and system() curl, and the two showed similar performance, measured in request response time. The results are same localserver and google endpoints requests.

In terms of usage and flexibility, cURLpp has much to offer.

For those interested, this is the cURLpp body for a google Drive multipart upload.

std::istringstream curlBody(metadata.str()+body2.str());
int size = curlBody.str().size();
try {
curlpp::Cleanup cleaner;
curlpp::Easy req;
Wt::log("info")<<"Curlpp upload to google";
std::list<std::string> header;
header.push_back("Content-Type: multipart/related;boundary=testing");
header.push_back("Authorization: Bearer "+access_token);
req.setOpt(new HttpHeader(header));
req.setOpt(new Url("https://www.googleapis.com/upload/drive/v2/files/"+fileID+"?uploadType=multipart"));
req.setOpt(new Verbose(true)); //Make it talk a lot
req.setOpt(new Put(true));
req.setOpt(new ReadStream(&curlBody));
req.setOpt(new InfileSize(size));
req.perform();
Wt::log("curl") << "DONE";
} catch ( curlpp::LogicError & e ) {
Wt::log("error") << e.what();
} catch ( curlpp::RuntimeError & e ) {
Wt::log("error") << e.what();
}

In the cURLpp example04, a getInfo function is shown and I am eager to test out the feature.

Leave a Reply

Your email address will not be published. Required fields are marked *