An example of how to make an asynchronous HTTP get request using cpp-netlib

I am trying to execute asynchronous HTTP requests using cpp-netlib . I could not find any examples of this in the documentation, as a result I could not even compile it. My current attempt is below (with compilation errors in the comments). Any tips on 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; } 
+4
source share
1 answer

I have not used cpp-netlib, but it looks like you have some obvious problems with your code:

The first error is the lack of boost:: typedef function.

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

Must be

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

Second error:

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

There 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; } 

The third mistake is that you should pass the callback, not call it:

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

Must be

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

Hope this is all (or enough to get you started).

+3
source

Source: https://habr.com/ru/post/1441947/


All Articles