Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I extract the http response when using libcurlpp?

Tags:

libcurl

curlpp

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.

like image 935
Stéphane Avatar asked Feb 15 '11 20:02

Stéphane


2 Answers

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().

like image 77
Stéphane Avatar answered Sep 24 '22 03:09

Stéphane


Maybe curlpp have been updated since the question was asked. I'm using this which I found in example04.cpp.

#include <curlpp/Infos.hpp>

long http_code = 0;
request.perform();
http_code = curlpp::infos::ResponseCode::get(request);
if (http_code == 200) {
    std::cout << "Request succeeded, response: " << http_code << std::endl;
} else {
    std::cout << "Request failed, response: " << http_code << std::endl;
}
like image 40
kometen Avatar answered Sep 24 '22 03:09

kometen