Four years ago I wrote a blog post that shown how to build a web server using the http_listener from the C++ REST SDK library as well as a client application that consumed the exposed resources. Over the years there have been various changes to the API from the library and some readers complained the code no longer compiled. Eventually, I decided to revisit that post and update my code to the latest version of the library, which at this time is 2.10.

I will not reiterate all the details described in the former article. However, in summary, the server maintains a dictionary of values (both keys and values are strings). Through HTTP calls a client can retrieve the content of the dictionary, add new values, update or delete existing ones.

HTTP method Description Request Response
GET retrieves all the key-value pair from the dictionary {"one" : "100", "two" : "200"}
POST retrieves the values of the specified keys from the dictionary ["one", "two", "three"] {"one" : "100", "three" : "", "two" : "200"}
PUT inserts new pairs of key-values in the dictionary; if a key is already found its value is updated {"one" : "100", "two" : "200"} {"one" : "", "two" : ""}
DELETE deletes the specified keys from the dictionary ["one"] {"one" : ""}

Here is the server code:

And this is the client code:

Notice there are slight changes in the way output is formatted, both in the server and the client application. The rest is mostly unchanged, except for the handling of JSON, that has changed significatly since version 1.1. Again, please see the original post for an explanation of the code.

The output from running these client and server applications is shown below. On the left is the client output, and on the right the server output.

, , , Hits for this post: 5137 .

6 comments untill now

  1. […] UPDATE: for an updated version of this demo, using C++ REST SDK 2.10 see this blog post. […]

  2. Check out my C++ REST SDK Client Server app if you are interested

  3. Seong-Cheol Kim @ 2018-01-02 12:55

    int main()
    http_listener listener(L”http://localhost/restdemo”);

  4. I would like to know how to call via a browser without using my client, http://localhost/restdemo?aaa=3

  5. Neither the server nor the client works on ubuntu 16.04

    g++ -std=c++11 server.cpp -o server -lboost_system -lcrypto -lssl -lcpprest -pthread

  6. Hi,

    I would like know how to create https server and client

Add your comment now