First, the HTTPGetAsync method creates a new instance of the web::http::client:http_client class to maintain a connection with the REST API whose base URI is http://localhost:58957/api/. Then, the method manually builds up an HTTP GET request with a header that specifies the content type (application/json) and sets the request URI to groups. This way, the HTTP GET request will be made to http://localhost:58957/api/groups.

The following lines show the initial code that makes the request and return the resulting task instance that represents the asynchronous operation. The request method uses the previously built http_request instance (request) as an argument. Notice that the then method adds a continuation task to the task that executes the request. This task receives an http_response instance (response) and returns a pplx::task<json::value>, i.e., a task that will return a json::value.

After the request is processed, the code after the then continuation will be executed in a new task. If the status code of the response is OK, the code returns the result of the response.extract_json() method. This method extracts the body of the response message into a JSON value, checking that the content type is application/json. The method does the extraction in a new task and returns a pplx::task<json::value> instance.

There is another then method that adds a continuation task to the one that analyzes the response. This task receives a pplx::task<json::value> instance (previousTask) and starts executing when the JSON value is available. The code calls the previousTask.get method to retrieve the json::value extracted from the body of the response and uses it as an argument to call the DisplayJSONValue method.

The JSON response includes nested objects, so the DisplayJSONValue method is prepared to deal with them. First, the method checks whether the received json::value (v) is null or not. Then, the code uses the cbegin and cend methods combined with the for statement to loop over each element in the object. The cbegin method gets the beginning const iterator element for a composite JSON value and the cend method gets the end const iterator element.

for (auto iter = v.cbegin(); iter != v.cend(); ++iter)

The code within the for loop makes sure that it gets the values as const references in order to avoid copying the whole JSON value recursively, a situation that would be too expensive for nested objects. The code retrieves the first stored value (key) and the second stored value (value) for each element.

Then, the code checks whether the retrieved value is either an object (value.is_object() returns true) or an array (value.is_array() returns true). If this condition evaluates to true, the value is an object with children or an array, and therefore, the code displays the key as a string to indicate the parent's element name that will be listed when the key isn't null. Then, the code must loop over each element in the object or array, and therefore, it just calls the DisplayJSONValue method with the retrieved value as an argument. This way, with just a few lines of code, the different chained asynchronous methods analyze the response, extract the body of the response into a JSON value and iterate over the different nested JSON elements to display their keys and values. When the value isn't either an object or an array, the code displays both the key and the value as strings. The JSON response used in the example is a bit complex in order to represent a real-life JSON response and not just a few simple values without nested objects. In fact, arrays and nested objects make things a bit more complicated and the example demonstrates that you can easily work with them with the different methods in json::value.

As you work with many chained asynchronous tasks, you might be wondering about the difficulties when debugging the different pieces of code that execute on diverse tasks. The debugging features available in Visual Studio 2012 didn't provide all the necessary information for developers to understand the state of asynchronous code execution nor easily find and fix the asynchronous code. However, Visual Studio 2013 introduced async-aware debugging and made many changes to the different debugging windows that developers were accustomed to watching in Visual Studio 2012.

Visual Studio 2013 makes it easier for you to debug the asynchronous code that you usually write when working with C++ REST SDK. Figure 1 shows the enhanced Call Stack window in Visual Studio 2013 that displays allows you to know how you arrived to the actual line within the DisplayJSONValue. If you want to dive deeper on the new Visual Studio 2013 async-aware debugging features, you can read Visual Studio 2013 Asynchronous Debugging. That article is in C#, but you can use the same features in your C++ code with the asynchronous calls generated with the C++ REST SDK.

Creating JSON Objects

Creating JSON objects with the C++ REST SDK is not as easy as it is with JavaScript, however, the boilerplate is significantly reduced in C++. The following lines show a sample CreateAndDisplayJSONObject method that creates a JSON object with many keys and values that were part of the sample JSON response.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!