Revisited: Full-fledged client-server example with C++ REST SDK 2.10

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.

21 Replies to “Revisited: Full-fledged client-server example with C++ REST SDK 2.10”

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

  2. 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

  3. It is not something cpprest needs to support. You can consume a eeb service using any programming language or tool that supports that. So, of course you can consume it from .Net.

  4. Just wanted to make note that wcout is required on Windows, since all strings are utf-16. If you’re on linux systems, cpprest uses utf-8 strings internally and you need to use cout instead.

  5. hi, first of all thank you so much for your valuable blog! it has been really helping. it worked with my application just with minor changes. But still I have a doubt about receiving the response from the server….. how does the get function work with nullvalue???? what in case I want to receive another processed response from my application

  6. Hi! First of all, thank you so much!

    I have compiled your client code. When running it, the output says “Incorrect Content-Type: must be textual to extract_string, JSON to extract_json.”.Also, it shows the json, but no in the correct order.

    Any ideas?


  7. Anyone have an idea why I could receive GET and PUT messages but not DEL messages from http client from Chrome browser?
    I know it’s CORS related somehow.
    My server does this:

    void WebAPIController::initRestOpHandlers() {, std::bind(&WebAPIController::handleGet, this, std::placeholders::_1));, std::bind(&WebAPIController::handlePut, this, std::placeholders::_1));, std::bind(&WebAPIController::handlePost, this, std::placeholders::_1));, std::bind(&WebAPIController::handleDelete, this, std::placeholders::_1));, std::bind(&WebAPIController::handlePatch, this, std::placeholders::_1));

    when I put a breakpoint in the hander for the DEL request, it never get’s called.
    the GET and PUT and POST for that matter, do.

    any help is greatly appreciated.


  8. Hi Marius,

    Do you have some idea on how to check if in server side, the received file is a json or another extension ?
    Like if we want to send an image or another media type trow this webservice ?

    Thanks for your reply

  9. Hii, I would like to know if anyone test this example sucessfully on ubuntu?? I get a lot of errors when compiling on ubuntu 16.04 and 18.04. I would appreciate any recomendation, thankss

  10. with HTTPS, I get the error :


    for the url of the format:

    Could you please suggest a solution please

  11. our server only listens to https
    If we try http from client, we get the response:
    WinHttpReceiveResponse: 12002: The operation timed out

    If we try https from client, we get the response:

    Please help.

  12. Hello, I’d love to learn this library but I can’t get my hands on learning materials for writing the server code. Any help?

  13. Hi,
    I have installed cpprestsdk using vcpkg on windows 10 and built the c++ project using Visual Studio 2019. The program build without any errors.
    But the program is not running and exiting with below error, when I run.

    The thread 0x63c4 has exited with code 0 (0x0).
    Exception thrown: write access violation.
    this->**_Myproxy** was 0x700074.

    anyone encountered similar problem?

  14. If you put the effort into developing your own http server why would you waste your time using rest?, why not just develop your own services architecture?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.