Example of asynchronous HTTP GET request using cpp-netlib #160

Closed
armansu opened this Issue Oct 25, 2012 · 3 comments

Projects

None yet

2 participants

@armansu
armansu commented Oct 25, 2012

I'm trying to implement asynchronous HTTP GET request using cpp-netlib. My first attempt is below. It compiles, but doesn't produce any output. Any idea how to make it work? Code sample will be helpful. Thank you in advance!

#include <iostream>
#include <boost/network/protocol/http/client.hpp>
#include <boost/range/iterator_range.hpp>

using namespace std;
using namespace boost::network;
using namespace boost::network::http;

void callback(boost::iterator_range<char const *> const &, boost::system::error_code const &)
{
    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, callback);
    if (ready(response))
    {
        cout << body(response) << endl;
    }
    return 0;
}
@deanberris
Member

The problem here is that you have a race condition. The callback doesn't
get called by the time your main function returns.

What you want to do is to do "cout << body(response) << endl;" without
checking if the response is ready. Note that "ready(response)" doesn't
block.

On Thu, Oct 25, 2012 at 12:13 PM, armansu notifications@github.com wrote:

I'm trying to implement asynchronous HTTP GET request using cpp-netlib. My
first attempt is below. It compiles, but doesn't produce any output. Any
idea how to make it work? Code sample will be helpful. Thank you in advance!

#include
#include
#include

using namespace std;
using namespace boost::network;
using namespace boost::network::http;

void callback(boost::iterator_range const &, boost::system::error_code
const &)
{
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, callback);
if (ready(response))
{
cout << body(response) << endl;
}
return 0;
}


Reply to this email directly or view it on GitHubhttps://github.com/cpp-netlib/cpp-netlib/issues/160.

Dean Michael Berris | Software Engineer
Google

@armansu
armansu commented Oct 25, 2012

Thanks for a quick reply! Any hints how to implement a simple asynchronous client using cpp-netlib. I assume, it won't be as long as boost asio async client. Code samples will be helpful. My pathetic attempt is below.

#include <iostream>
#include <string>
#include <boost/network/protocol/http/client.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/network/protocol/http/response.hpp>
#include <boost/network/protocol/http/client/connection/connection_delegate_factory.hpp>
#include <boost/network/protocol/http/traits/delegate_factory.hpp>
#include <boost/network/protocol/http/client/connection/async_normal.hpp>

using namespace std;
using namespace boost::network;
using namespace boost::network::http;

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

std::string res;

void callback(boost::iterator_range<char const *> const & a, boost::system::error_code const & b)
{
    for(rValue::difference_type i = 0; i < a.size(); ++i)
    {
        res += a[i];
    }
}

int main() {
    http::client client;
    http::client::request request("http://www.blekko.com/");
    http::client::response response = client.get(request, callback);
    while (!ready(response));
    cout << res << endl;
    return 0;
}
@deanberris
Member

If you do the printing inside the callback you should be fine.

On Fri, Oct 26, 2012 at 9:59 AM, armansu notifications@github.com wrote:

Thanks for a quick reply! Any hints how to implement a simple asynchronous
client using cpp-netlib. I assume, it won't be as long as boost asio
async clienthttp://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/example/http/client/async_client.cpp.
Code samples will be helpful. My pathetic attempt is below.

#include #include #include <boost/network/protocol/http/client.hpp>#include <boost/range/iterator_range.hpp>#include <boost/network/protocol/http/response.hpp>#include <boost/network/protocol/http/client/connection/connection_delegate_factory.hpp>#include <boost/network/protocol/http/traits/delegate_factory.hpp>#include <boost/network/protocol/http/client/connection/async_normal.hpp>
typedef boost::function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type;typedef boost::iterator_range<char const *> rValue;
std::string res;
void callback(boost::iterator_range<char const *> const & a, boost::system::error_code const & b){
for(rValue::difference_type i = 0; i < a.size(); ++i)
{
res += a[i];

}}

int main() {
http::client client;

http::client::request request("http://www.blekko.com/");

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

while (!ready(response));
cout << res << endl;
return 0;}


Reply to this email directly or view it on GitHubhttps://github.com/cpp-netlib/cpp-netlib/issues/160#issuecomment-9797541.

Dean Michael Berris | Software Engineer
Google

@deanberris deanberris closed this Jul 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment