Skip to content

Commit bb97e54

Browse files
committed
Fixes cpp-netlib#194 -- show example of proper client usage and error handling.
1 parent b52fe01 commit bb97e54

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

libs/network/doc/reference/http_client.rst

+20
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ happens on a different thread.
105105
dedicated to that request. The client does not re-cycle connections and uses
106106
a one-request-one-connection model.
107107

108+
When an asynchronous client object is destroyed, it waits for all pending
109+
asynchronous operations to finish. Errors encountered during operations on
110+
retrieving data from the response objects cause exceptions to be thrown --
111+
therefore it is best that if a client object is constructed, it should outlive
112+
the response object or be outside the try-catch block handling the errors from
113+
operations on responses. In code, usage should look like the following:
114+
115+
.. code-block:: c++
116+
117+
http::client client;
118+
try {
119+
http::client::response response = client.get("http://www.example.com/");
120+
std::cout << body(response);
121+
} catch (std::exception& e) {
122+
// deal with exceptions here
123+
}
124+
125+
A common mistake is to declare the client inside the try block which invokes
126+
undefined behavior when errors arise from the handling of response objects.
127+
108128
Member Functions
109129
----------------
110130

0 commit comments

Comments
 (0)