libcurlcurlpp

how do I extract the http response when using libcurlpp?


Trying to use libcurlpp (a C++ wrapper to libcurl) to post a form and get the response. It all works, but I have no idea how to programmatically get access to the response from the curlpp::Easy object after the http transaction has finished. Bascially:

#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
...
curlpp::Easy foo;
foo.setOpt( new curlpp::options::Url( "http://example.com/" ) );
foo.setOpt( new curlpp::options::Verbose( true ) );
...many other options set...
foo.perform();  // this executes the HTTP transaction

When this code runs, because Verbose is set to true I can see the response get output to STDOUT. But how do I get access to the full response instead of having it dump to STDOUT? The curlpp::Easy doesn't seem to have any methods to gain access to the response.

Lots of hits in Google with people asking the same question, but no replies. The curlpp mailing list is a dead zone, and API section of the curlpp web site has been broken for a year.


Solution

  • This is how I finally did it:

    // HTTP response body (not headers) will be sent directly to this stringstream
    std::stringstream response;
    
    curlpp::Easy foo;
    foo.setOpt( new curlpp::options::Url( "http://www.example.com/" ) );
    foo.setOpt( new curlpp::options::UserPwd( "blah:passwd" ) );
    foo.setOpt( new curlpp::options::WriteStream( &response ) );
    
    // send our request to the web server
    foo.perform();
    

    Once foo.perform() returns, the full response body is now available in the stream provided in WriteStream().