The C++ REST project provides a modern asynchronous C++ API for cloud-based client-server communication. It is available on codeplex and works with Visual Studio 2010 and 2012, but it is now distributed with Visual Studio 2013. The preview version of VS2013 deploys version 1.0 of the SDK. Since its release a new version of the SDK (v1.1.0) has been made available.

The following example shows how to retrieve and display some JSON content. I’m querying google using its REST API. Here is how a query (for a single result) may look:

The result that we get is a JSON value. From this value I will only display the URL and the (un-formatted) title.

json sample result { "responseData":{ "results":[ { "GsearchResultClass":"GwebSearch", "unescapedUrl":"http://www.mariusbancila.ro/", "url":"http://www.mariusbancila.ro/", "visibleUrl":"www.mariusbancila.ro", "cacheUrl":"http://www.google.com/search?q\u003dcache:oFoVQN2DkeYJ:www.mariusbancila.ro", "title":"\u003cb\u003eMarius Bancila\u0026#39;s\u003c/b\u003e Blog", "titleNoFormatting":"Marius Bancila\u0026#39;s Blog", "content":"\u003cb\u003eMarius Bancila\u003c/b\u003e Tools, Visual Studio Add your comment. Visual Studio 2013 provides developers with the ability to sign in with a Microsoft account and \u003cb\u003e...\u003c/b\u003e" } ], "cursor":{ "resultCount":"6,850", "pages":[ { "start":"0", "label":1 }, { "start":"1", "label":2 }, { "start":"2", "label":3 }, { "start":"3", "label":4 }, { "start":"4", "label":5 }, { "start":"5", "label":6 }, { "start":"6", "label":7 }, { "start":"7", "label":8 } ], "estimatedResultCount":"6850", "currentPageIndex":0, "moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dmarius+bancila", "searchResultTime":"0.16" } }, "responseDetails":null, "responseStatus":200 } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 { "responseData" : { "results" : [ { "GsearchResultClass" : "GwebSearch" , "unescapedUrl" : "http://www.mariusbancila.ro/" , "url" : "http://www.mariusbancila.ro/" , "visibleUrl" : "www.mariusbancila.ro" , "cacheUrl" : "http://www.google.com/search?q\u003dcache:oFoVQN2DkeYJ:www.mariusbancila.ro" , "title" : "\u003cb\u003eMarius Bancila\u0026#39;s\u003c/b\u003e Blog" , "titleNoFormatting" : "Marius Bancila\u0026#39;s Blog" , "content" : "\u003cb\u003eMarius Bancila\u003c/b\u003e Tools, Visual Studio Add your comment. Visual Studio 2013 provides developers with the ability to sign in with a Microsoft account and \u003cb\u003e...\u003c/b\u003e" } ] , "cursor" : { "resultCount" : "6,850" , "pages" : [ { "start" : "0" , "label" : 1 } , { "start" : "1" , "label" : 2 } , { "start" : "2" , "label" : 3 } , { "start" : "3" , "label" : 4 } , { "start" : "4" , "label" : 5 } , { "start" : "5" , "label" : 6 } , { "start" : "6" , "label" : 7 } , { "start" : "7" , "label" : 8 } ] , "estimatedResultCount" : "6850" , "currentPageIndex" : 0 , "moreResultsUrl" : "http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dmarius+bancila" , "searchResultTime" : "0.16" } } , "responseDetails" : null , "responseStatus" : 200 }

There are several things that we must do:

create a http_client that maintains the connection to a HTTP service

that maintains the connection to a HTTP service send a HTTP GET request asynchronous and wait for the response

when the response is available check its status code and if all is right extract the JSON value from the body of the response message (the content type must be application/json )

) when the json value is available display its content

The code looks like this:

#include <cpprest/http_client.h> #include <cpprest/filestream.h> #include <cpprest/json.h> #include <iostream> using namespace utility; using namespace web; using namespace web::http; using namespace web::http::client; using namespace concurrency::streams; using namespace std; void print_search_results(json::value const & value) { if(!value.is_null()) { auto response = value[L"responseData"]; auto results = response[L"results"]; for(auto const & p : results) { auto o = p.second; auto url = o[L"url"]; auto title = o[L"titleNoFormatting"]; wcout << title.as_string() << endl << url.as_string() << endl << endl; } } } void search_and_print(wstring const & searchTerm, int resultsCount) { http_client client(U("https://ajax.googleapis.com/ajax/services/search/web")); // build the query parameters auto query = uri_builder() .append_query(L"q", searchTerm) .append_query(L"v", L"1.0") .append_query(L"rsz", resultsCount) .to_string(); client // send the HTTP GET request asynchronous .request(methods::GET, query) // continue when the response is available .then([](http_response response) -> pplx::task<json::value> { // if the status is OK extract the body of the response into a JSON value // works only when the content type is application\json if(response.status_code() == status_codes::OK) { return response.extract_json(); } // return an empty JSON value return pplx::task_from_result(json::value()); }) // continue when the JSON value is available .then([](pplx::task<json::value> previousTask) { // get the JSON value from the task and display content from it try { json::value const & v = previousTask.get(); print_search_results(v); } catch (http_exception const & e) { wcout << e.what() << endl; } }) .wait(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <cpprest/http_client.h> #include <cpprest/filestream.h> #include <cpprest/json.h> #include <iostream> using namespace utility ; using namespace web ; using namespace web : : http ; using namespace web : : http : : client ; using namespace concurrency : : streams ; using namespace std ; void print_search_results ( json : : value const & value) { if(!value.is_null()) { auto response = value[L"responseData"]; auto results = response [ L "results" ] ; for ( auto const & p : results) { auto o = p.second; auto url = o [ L "url" ] ; auto title = o [ L "titleNoFormatting" ] ; wcout < < title . as_string ( ) < < endl < < url . as_string ( ) < < endl < < endl ; } } } void search_and_print ( wstring const & searchTerm, int resultsCount) { http_client client(U("https://ajax.googleapis.com/ajax/services/search/web")); // build the query parameters auto query = uri_builder ( ) . append_query ( L "q" , searchTerm ) . append_query ( L "v" , L "1.0" ) . append_query ( L "rsz" , resultsCount ) . to_string ( ) ; client // send the HTTP GET request asynchronous . request ( methods : : GET , query ) // continue when the response is available . then ( [ ] ( http_response response ) - > pplx : : task < json : : value > { // if the status is OK extract the body of the response into a JSON value // works only when the content type is application\json if ( response . status_code ( ) == status_codes : : OK ) { return response . extract_json ( ) ; } // return an empty JSON value return pplx : : task_from_result ( json : : value ( ) ) ; } ) // continue when the JSON value is available . then ( [ ] ( pplx : : task < json : : value > previousTask ) { // get the JSON value from the task and display content from it try { json : : value const & v = previousTask.get(); print_search_results ( v ) ; } catch ( http_exception const & e) { wcout << e.what() << endl; } } ) . wait ( ) ; }

The following program retrieves 5 search results for “marius bancila”.

int main(int argc, char *args[]) { search_and_print(L"marius bancila", 5); return 0; } 1 2 3 4 5 6 int main ( int argc , char * args [ ] ) { search_and_print ( L "marius bancila" , 5 ) ; return 0 ; }

The output looks like this:

Marius Bancila's Blog http://www.mariusbancila.ro/ Marius Bancila (mariusbancila) on Twitter https://twitter.com/mariusbancila User Marius Bancila - Stack Overflow http://stackoverflow.com/users/648078/marius-bancila Marius Bancila profiles | LinkedIn http://www.linkedin.com/pub/dir/Marius/Bancila Marius Bancila Profiles | Facebook https://www.facebook.com/public/Marius-Bancila 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Marius Bancila ' s Blog http : //www.mariusbancila.ro/ Marius Bancila ( mariusbancila ) on Twitter https : //twitter.com/mariusbancila User Marius Bancila - Stack Overflow http : //stackoverflow.com/users/648078/marius-bancila Marius Bancila profiles | LinkedIn http : //www.linkedin.com/pub/dir/Marius/Bancila Marius Bancila Profiles | Facebook https : //www.facebook.com/public/Marius-Bancila

You can find documentation for the API and the SDK here.

Share this: Facebook

Twitter

Print

More

Email

Reddit



