Claire Andrews
Claire Andrews

Reputation: 301

Async client response with cpp-netlib?

I am considering using cpp netlib for a new project. All of the examples show reading the body of the response in a blocking manner:

client::response response_ = client_.get(request_);
std::string body_ = body(response_);

If I construct my client object with the async tag:

basic_client<http_async_8bit_udp_resolve_tags, 1, 1> client_();

What affect does that have?

Is it possible to get the results of the body wrapper as a boost::shared_future<std::string>?

Do I just need to wrap the blocking call in it's own thread?

Upvotes: 3

Views: 819

Answers (1)

olegabr
olegabr

Reputation: 545

Look at the current http client doc: http://cpp-netlib.org/0.12.0/reference/http_client.html

  1. the http client is now always async by default
  2. you have an option to provide a callback function or object in the get or post call:

    struct body_handler {
    
        explicit body_handler(std::string & body)
        : body(body) {}
    
        BOOST_NETWORK_HTTP_BODY_CALLBACK(operator(), range, error) {
            // in here, range is the Boost.Range iterator_range, and error is
            // the Boost.System error code.
            if (!error)
                body.append(boost::begin(range), boost::end(range));
        }
    
        std::string & body;
    };
    
    // somewhere else
    std::string some_string;
    response_ = client_.get(request("http://cpp-netlib.github.com/"),
                        body_handler(some_string));
    
  3. The client::response object already incapsulates the future object:

The response object encapsulates futures which get filled in once the values are available.

Upvotes: 1

Related Questions