Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
}
share|improve this question

1 Answer

up vote 2 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.