Issue link:

Contents of this Issue


Page 15 of 15

In this case, the code defined in the then continuation displays the status code returned by the server, the headers content type and length. Everything is easy to access by using the http_response received as a parameter (response) to the continuation task. std::wostringstream stream; std::wostringstream stream; stream << L"Server returned returned status code " << response.status_code() << L'.' << std::endl; std::wcout << stream.str(); stream.str(std::wstring()); stream << L"Content type: " << response.headers().content_ type() << std::endl; stream << L"Content length: " << response.headers().content_ length() << L"bytes" << std::endl; std::wcout << stream.str(); Here, the simple echo call to the Flickr API returns the following XML response. The example just wants to introduce how to build a simple HTTP GET request with parameters and process some results with the C++ REST SDK. The code that reads the response body uses a stream to retrieve the data from the incoming request. In this case, to keep things simple, the code calls the read_to_end method and then chains a call to the get method in order to wait for the results within the task in which the code is executing. The read_to_end method reads until reaching the end of the stream and returns a task that contains the two files the number of characters read. Thus, you can call the read_to_end method with an asynchronous execution and chain another task to process the read results with the then continuation. In this case, the get method waits for the task to finish and returns the result that the task produced. Notice that when a task is canceled, a call to the get method will throw a task_canceled exception. You usu- ally won't want to use get if you aren't within another task (so as to avoid blocking the UI). auto bodyStream = response.body(); streams::stringstreambuf sbuffer; auto& target = sbuffer.collection(); bodyStream.read_to_end(sbuffer).get(); stream.str(std::wstring()); stream << L"Response body: " << target.c_str(); std::wcout << stream.str(); As you can see from this example, executing a simple HTTP GET re- quest with C++ 11 and the C++ REST SDK is fairly straightforward. The then continuation makes it simple to understand how asynchronous calls are chained and the different helpers included in the C++ REST SDK reduce the necessary boilerplate code to the minimum. Gastón Hillar is a frequent contributor to Dr. Dobb's. Previous Next Previous Next Download Download Register Register Subscribe Subscribe Previous Next Previous Next [C++REST SDK] June 2014 16 Comment

Articles in this issue

Links on this page

view archives of InformationWeek - Dr_Dobbs_TD_5_14