3

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.