Example on how to do asynchronous http get request using cpp-netlib

asynchronous, c++, compiler-errors, cpp-netlib, http-request

Solution

I've not used cpp-netlib, but it looks like there's some obvious issues with your code:

First error is a missing `boost::` on the function typedef.

typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token

Should be

typedef boost::function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type;

Second error is:

body_callback_function_type callback() 
{
    cout << "This is my callback" << endl;
}

Should be the right kind of function:

void callback( boost::iterator_range<char const *> const &, boost::system::error_code const &)
{
    cout << "This is my callback" << endl;
}

Third error is that you should pass the callback, not call it:

http::client::response response = client.get(request, http::_body_handler=callback());

Should be

http::client::response response = client.get(request, callback);

Hopefully thats all (or enough to get you started).

Problem

I'm trying to do asynchronous http requests using cpp-netlib. I couldn't find any examples of this in the documentation, as a result can't even get it to compile. My current attempt is below (with compilation errors in the comments). Any hints how to make it work? Thank you in advance! ``` #include <iostream> #include <boost/network/protocol/http/client.hpp> using namespace std; using namespace boost::network; using namespace boost::network::http; typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token body_callback_function_type callback() // ERROR: 'body_callback_function_type' does not name a type { cout << "This is my callback" << endl; } int main() { http::client client; http::client::request request("http://www.google.com/"); http::client::response response = client.get(request, http::_body_handler=callback()); // ERROR: 'callback' was not declared in this scope cout << body(response) << endl; return 0; } ```

Original source