C++ REST SDK in Visual Studio 2013

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:

https://ajax.googleapis.com/ajax/services/search/web?q=marius%20bancila&v=1.0&rsz=1

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

There are several things that we must do:

  • create a http_client 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:

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

The output looks like this:

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

, , , Hits for this post: 17828 .
Trackback

8 comments untill now

  1. Gravatar
    Bartosz Bielecki @ 2013-08-09 09:31

    Looking at the code I assume that by usage of .then() you are restricting the code to Windows platform, or there are some internal tricks to simulate it?

  2. Gravatar
    paercebal @ 2013-08-09 16:06

    @Bartosz Bielecki

    I could be wrong, but looking at the sources (which are freely available), then is a member function of class task, which is the type returned by request.

    The code:

    client.request(…).task(…) ;

    is simply chaining the calls. Not some compiler specific trick.

    I saw nothing in the sources that wouldn’t be compiled on any C++11 compliant compiler.

    P.S.: Sources:

    https://casablanca.codeplex.com/SourceControl/latest#Release/include/cpprest/http_client.h

    https://casablanca.codeplex.com/SourceControl/latest#Release/include/pplx/pplxtasks.h

  3. Gravatar

    […] support is also a key feature of the SDK, and this example from Marius Bancila’s Blog shows how to make an asynchronous REST call that returns […]

  4. Gravatar

    […] my previous post I shown how you can build a C++ application with the C++ REST STK that fetches search results from […]

  5. Gravatar

    Hi Marius,

    I’m hoping you can help me. I was conflicted whether to use the latest releases with VS 2012 Express, or go for VS 2013 Express RC, which includes Casablanca – albeit the 1.0 version.

    So I went with VS 2013 RC. I am trying to run the example code from here:
    http://msdn.microsoft.com/en-us/library/jj950081(v=vs.120).aspx

    but am having fits with my C++ project finding the appropriate library and specific library files. I am following steps as outlined here: http://stackoverflow.com/questions/4066405/when-to-use-the-visual-studio-additional-dependencies

    for setting up additional dependencies in my project. My project can now find the header files, but I am getting errors because it either can’t find or can’t open the actual library files (going from memory, I think they are named something like “casablanca120″).

    Did you run into this issue with VS 2013 RC? Could you share with me how you set up your project under Configuration Properties –> Linker?

    Thank you for any help or guidance you can provide.

    Terry

  6. Gravatar

    If you try a newer SDK with VS2013, even if you get the paths working, you won’t get it working, because the SDK does not have builds for VS2013 and your app will crash at runtime (http://casablanca.codeplex.com/workitem/49). If you use the SDK that comes with the Preview, then all paths should be set and the project should build. However, if that is not the case you can still set additional include and library directories as well as static library files to link with.

  7. Gravatar

    create an application that calls the rest api from one computer and the other computer responds
    with a message using c++ REST sdk.. please help me .. give me a coding..

  8. Gravatar

    Would you mind including a full sample project for download?

    I get build errors with this code. I think my project is screwed up. Maybe to do with unicode. (Probably because I have to set project settings manually as nuget doesnt seem to it for me).

    auto response = value[L"responseData"];

    error C2678: binary ‘[‘ : no operator found which takes a left-hand operand of type ‘const web::json::value’ (or there is no acceptable conversion)

Add your comment now