In my previous post I shown how you can build a C++ application with the C++ REST SDK that fetches search results from a search engine. In this post I will go a step further and develop a client-server application from scratch using version 1.1.0 of the SDK. This version features an HTTP listener implementation (still in an experimental phase). Notice that for the time being this 1.1.0 SDK release does not work with Visual Studio 2013 Preview. This samples and built with Visual Studio 2012.

Overview of the problem to solve

The server manages a dictionary of key-value pairs (both strings) and supports several HTTP request methods:

  • GET: retrieves all the key-value pair from the dictionary.
    The response is a JSON object representing key-value pairs (eg. {"one" : "100", "two" : "200"}).
  • POST: retrieves the values of the specified keys from the dictionary.
    The request is a JSON array of strings (eg. ["one", "two", "three"]).
    The response is similar to the GET method, except that only requested keys are returned.
  • PUT: inserts new pairs of key-values in the dictionary. If a key is already found its value is updated.
    The request is a JSON object representing pairs of keys and values (eg. {"one" : "100", "two" : "200"})
    The response is a JSON object representing they key and the result for the action, such as addition or update (eg. {"one" : "<put>", "two" : "<updated>"}).
  • DEL: deletes the specified keys from the dictionary.
    The request is a JSON array of strings (eg. ["one", "two", "three"]).
    The response is a JSON object representing they key and the result for the action, such as success or failure (eg. {"one" : "<deleted>", "two" : "<failed>"}).

Notice that the server implements both GET and POST. The GET method is supposed to request a representation of the specified URI. Though it is theoretically possible that a GET request carries a body, in practice that should be ignored. The C++ REST library actually triggers an exception if you make a GET request with a body. Therefore, GET is used to return the entire content of the dictionary and the POST method, that supports a body, returns only the requested key-value pairs.

The client can make HTTP requests to the server, adding or updating key-values, fetch or delete existing pairs.

All communication, both for the request and the answer, is done using JSON values.

The server implementation

On the server side we have to do the following:

  • instantiate an http_listener object, specifying the URI where it should listen for requests.
  • provide handlers for the HTTP request methods for the listener.
  • open the listener and loop to wait for messages.

The core of the server application is shown below (except for the request handlers).

In this simple implementation the dictionary is a std::map. Its content is not persisted to disk, it is reloaded each time the server starts.

Let’s now look at the handlers. As mentioned earlier the GET method is a bit different than the others. A GET request should return all the key-value pairs in the server’s dictionary. Its implementation looks like this:

What it does is iterating through the dictionary and putting its key-value pairs into a json::value::field_map. That object is then sent back the the client.

The POST, PUT and DEL methods are a bit more complicated, because they all receive a JSON value specifying either keys to fetch or delete or pairs of key-value to add or update in the dictionary. Since some code would get duplicated several times I have created a generic method for handling requests that takes a function that evaluates the JSON request value and builds the response JSON value.

The handlers for POST, PUT and DEL will then call this generic method providing a lambda with the actual core implementation of each request handling.

And that is all with the server.

The client implementation

On the client side we need a http_client object to make HTTP requests to the server. It has an overloaded method request() that allows specifying the request method, a path and a JSON value for instance. A JSON value is not sent if the method is GET (or HEAD). Since for each request the answer is a JSON value, I have created a method called make_request() that dispatches the request and when the response arrives it fetches the JSON value and displays it in the console.

The core of the client code looks like this:

In the main() function I then just make a series of requests to the server, putting, fetching and deleting key-values from the server’s dictionary.

The client and server in action

You need to start the server first and then run the client. The output from running the client is:

On the server console the output is:

, , , , , , Hits for this post: 44896 .

28 comments untill now

  1. Gravatar
    Fernando @ 2013-11-06 21:21

    How to put the listener to listen on

    like http_listener listener(L””);

    not work

  2. Gravatar

    IP address is used to designate an invalid address (usually used when a machine is not connected to a TCP/IP network). Why would you expect this to work?

  3. Gravatar
    Fernando @ 2013-11-11 13:12

    IP address is not invalid.

    It listen on all interfaces on *this* network (my networks).

  4. Gravatar
    Fernando @ 2013-11-11 20:12

    With http_listener listener(L”http://*:9000/restdemo”); it works fine.

  5. Gravatar
    Steven Starr @ 2013-11-12 23:43

    IP address is used to designate an invalid address (usually used when a machine is not connected to a TCP/IP network). Why would you expect this to work?

    Your joking right? There are literally hundreds of reasons why your wrong.

  6. Gravatar

    Address is a convention to designate ALL interfaces. In the use case posed by Steven, it would listen on all addresses including This convention has been in use for decades for *nix operating systems..

    It would be helpful to adhere to a widely used convention..

  7. Gravatar

    Sorry, as I can get the file http_client.h

  8. Gravatar

    why is this used?

    while (true); //line 38 in the first snippet


  9. Gravatar
    Rotem Bentzur @ 2014-05-31 09:58

    I can’t help but notice the busy wait in the server code. I’m referring to the while (true) statement?
    Why is it there? Any other way?

  10. Gravatar
    Igor Sadchenko @ 2014-07-19 21:14

    I have this error
    no callable ‘begin’ function found for type ‘web::json::value’

  11. Gravatar

    can you share a working sln file solution for this example. did you build it in visual studio 2010? will it work in visual studio 2010? I copied the code and tried to compile there were numerous syntax errors. not to say the code is wrong but probably I don’t know how to set it up on platform.

  12. Gravatar

    Somebody can help me get IP Address connection from http_request

  13. Gravatar
    Abhijeet Badurkar @ 2015-07-03 11:13

    I am trying to connect the server with client on Android. For that I am using class HttpUrlConnection.
    I am posting data using OutputStreamWriter. But it is not working. Do you have any clues?



  14. Gravatar

    I am new to REST API.
    How can I build and test the mentioned application. Can I build it on visual studio 2013 ?

    I think I need to install the C++ SDK first before build.
    Could anybody direct me in getting the right C++ SDK.

    Thanks in advance,

  15. Gravatar
    Dieter Radler @ 2015-10-29 11:16

    Thanks for this great example.
    Gave me a jumpstart for solving my similar problem!

  16. Gravatar
    Hassan Ahmad @ 2015-12-12 22:26

    I am also getting the error
    no callable ‘begin’ function found for type ‘web::json::value’
    Could anyone help me. I am using Visual Studio 2013 with Nuget C++ rest sdk 2.7.0.

    Please please reply…its urgent.

  17. Gravatar
    Gene Norton @ 2016-05-04 15:34

    Is there any chance you could update your code to work with the current C++ REST SDK version 2.8?

    The JSON classes have changed.

  18. Gravatar
    Igor Pundev @ 2016-06-03 16:25

    Here is code, for both client and server, that working on REST SDK 2.8:
    server –
    client –
    Though it would be good that author updated this tutorial.

  19. Gravatar
  20. Gravatar
    thulasiv @ 2016-06-16 10:27

    can i use the server implemented (methods) here as a web service by hosting it.
    If so, can someone tell me please, how to do ?

  21. Gravatar

    There is no json::value::field_map type in the current stable version of cpprestsdk, so unfortunately I cannot build the example out of the box 🙁 It will be great if you adapt it to the current state of library code. Anyway, huge thanks for the tutorial!

  22. Gravatar
    thulasiv @ 2016-06-30 07:58


    I want to access the C++ Rest Web Service given in this tutorial using Java Client.
    How can i do, can someone help in this regard.

  23. Gravatar
    Francisco @ 2016-09-29 22:53

    Hello Mario,

    This tutorial was very useful. Do you know any technique to reduce cpu operations? By what I saw, the while loop is consuming alot of cpu operations. Just another question, how the reset is seen by the market today?

    Thank you,

  24. Gravatar
    Yongwei Wu @ 2017-01-04 15:14

    “while(true);” is not good: it takes unnecessary CPU cycles. There should at least something like “Sleep/sleep” in the loop.

    During my test, I also changed the following:

    json::value::field_map to json::value
    .push_back(make_pair(json::value(L"key"), json::value(L"value")) to [L"key"] = json::value(L"value")
    jvalue to jvalue.as_object() when it is being iterated over

  25. Gravatar

    Thank you Marius and Igor. Here is another update to this code for 2.9

    server –

  26. Gravatar

    Now, this server uses a different HTTP method for each API. Now, let’s say you have a trading terminal with a massive number of different APIs, how would this problem be solved ?

  27. Gravatar

    Very useful, thanks!
    Could you please show how to add unit tests (may be using native c++ unit test framework) to verify these functions and the way to mock http_request/http_response?

  28. Gravatar
    Mahaveer @ 2017-05-02 23:30

    How can I cancel the ongoing http request from the client side?

Add your comment now