Tag Archives: C++

Making a POST call with JSON data using CPPREST C++ SDK

The source code here is the same as the C# version in the previous post. The client makes a simple call like that: 

	std::string value2 = "azertyuiopqsdfghjklmwxcvbn";
	std::string buffer = Base64Helper::base64_encode((const unsigned char*)value2.c_str(), value2.length());
	std::wstring value3(buffer.begin(), buffer.end());
	SetData(key, value3, value3.length(), dbname);


Here is the wrapper code for SetData. It’s included in HttpLMDB dll: 

bool HTTPLMDB_API SetData(std::wstring key, std::wstring valueb64, DWORD dwLen, std::wstring name)
	std::wstring port = Constants::MasterNodePort;
	std::wstring ip = ServerHelper::GetIP();
	std::wstring url = ServerHelper::BuildURL(ip, port);

	std::wstring contentType = _T("Content-Type");
	std::wstring contentTypeV = _T("application/json");
	std::wstring keepAlive = _T("Keep-Alive");
	std::wstring keepAliveV = _T("false");
	std::wstring contentLength = _T("Content-Length");
	std::wostringstream bufLen;
	bufLen << contentType.length() + contentTypeV.length() + keepAlive.length() + keepAliveV.length() + contentLength.length() + 4;
	std::wstring len = bufLen.str().c_str();

	std::wostringstream buf;
	buf << url << '/' << Constants::Request << Constants::VerbSetDataB64
		<< _T("&name=") << name;
	url = buf.str().c_str();	
	std::wostringstream bufjson;
	bufjson << "{" << '"' << "key" << '"' << ":" << '"' << key << '"' << ","
		<< '"' << "value" << '"' << ":" << '"' << valueb64 << '"' << '}';
	std::wstring jsonv = bufjson.str().c_str();
	//wcout << _T("jsonv : ") << jsonv << endl;

	http_client client_lmdb(url);
	http_request request(methods::POST);
	request.headers().add(contentType, contentTypeV);
	request.headers().add(keepAlive, keepAliveV);
	request.headers().add(contentLength, len);

	http_response response;
	response = client_lmdb.request(request).get();

	wcout << response.to_string() << endl;

	return true;


Here is the code. It’s not very difficult.


Handling a long url using POST verb

To be able to store data in my LMDB Service, I need to store data as base64 items. To do that, I need to transmit json data from the client to the server. It can’t be passed on the url. So I use post handling. Here is the C++ handler:

void TheServer::handle_post(http_request message)


             std::wstring request = ServerHelper::FindParameter(message, _T("request"));
              if (request == Constants::VerbSetDataB64)
             else if (request == Constants::VerbGetDataB64)
                    // Does not work yet
       catch (...)
             // an internal problem occured
             g_Logger.WriteLog(_T("handle_post exception..."));


The C++ routine here to analyze is RequestVerbSetData64. Here is the code:

void TheServer::RequestVerbSetData64(http_request message)
       CLMDBWrapper lmdb;
       std::wstring dbNameW = ServerHelper::FindParameter(message, _T("name"));

       std::string dbName(dbNameW.begin(), dbNameW.end());
       std::wstring json;
       web::json::value jsonV = message.extract_json().get();

       Data data = Data::FromJSON(jsonV.as_object());
       TCHAR sz[255];
       _stprintf_s(sz, _T("Data key:%s value:..."), data.key.c_str());

       if (lmdb.Init((LPSTR)dbName.c_str()) == false)
             g_Logger.WriteLog(_T("LMDB Init not done !"));


       LPSTR lpszKey = W2A(data.key.c_str());
       LPSTR lpszValue = W2A(data.value.c_str());
       DWORD dwLen = strlen(lpszValue);

       lmdb.SetData(lpszKey, lpszValue, dwLen);




The source code is simple to write, simple to read. Because it is native code, it is fast and we just need to distribute the dll we use. here, it’s just the C runtime, the C++ runtime and the CPPREST dll. This is the advantage of the native stuff, you don’t need to distribute any framework that size is around 350 MB… It’s lightweight, it’s fast, it’s built on the metal.

Microsoft REST SDK Architecture

The architecture of the Microsoft CPPREST SDK is presented here for an article I write for french magazine Programmez.

It explains why the code is multi-platform. It’s based on Boost libraries. Boost is known to be a very good library, state of the art C++.

Article in Technical Magazine Programmez N° 219 – June 2018

May be in next June edition of french magazine Programmez, an article about creating a Windows Service. All the plumbing ! The second part will explain how to host a deamon inside and respond to external communication.

Create a simple cache in C# .NET

Here is a little sample of code I have written to simulate a cache, wich write the data every second on disk in the background in a dedicted thread.

Using this class Engine, you can write data to the internal dictionary and keep it safe because the database is written to disk… I do not have handled the reload of the data in the ctor… Here the client program who write any number of values you want:

If I run the console application using different values, here is the result:

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 10
Count: 10
Time elapsed set: 0 ms
Time elapsed get: 0 ms
Writing DB…
Writing DB OK

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 100
Count: 100
Time elapsed set: 0 ms
Time elapsed get: 0 ms
Writing DB…
Writing DB OK

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 1000
Count: 1000
Time elapsed set: 1 ms
Time elapsed get: 1 ms
Writing DB…
Writing DB OK

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 10000
Count: 10000
Time elapsed set: 7 ms
Time elapsed get: 13 ms
Writing DB…
Writing DB OK

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 100000
Count: 100000
Time elapsed set: 98 ms
Time elapsed get: 140 ms
Writing DB…
Writing DB OK

D:\Dev\MyCache\ConsoleApp1\bin\Debug>ConsoleApp1.exe 1000000
Count: 1000000
Writing DB…
Writing DB OK
Time elapsed set: 3656 ms
Time elapsed get: 4023 ms
Writing DB…
Writing DB OK

Source code here: MyCache.zip

Article in Technical Magazine Programmez N° 218 – May 2018

In May 2018, you will discover a technical article How to build under 64 bits environment. It contains tips&tricks for handling the migration because the source code will not compile as easy you can think…


LMDB for Windows

When I discovered the fantastic history of LMDB, It fascinated me. For a R&D projet, we need to present something that looks sexy and with a strong architecture. I proposed to study LMDB and bring to it the distributed engine and the clustering stuff with nodes. There are plenty ways of acheiveing this.

First, OpenLDAP-LMDB need to be ported on the Windows platform. It’s a C based code so there are no some many changes to fix for building on Windows. There was a path concat that made some problems but it’s defined as constants so I fixed it rapidly. I put it in Github after the operation was done.: https://github.com/ChristophePichaud/LMDBWindows

I was able to build a static lib for LMDB. But in Windows, we love shared modules called DLL so I have to put some little things on the source code to enable loading shared modules DLL (Dynamic Link Library).

#define LMDBWINDOWSDLL_API __declspec(dllexport)
#define LMDBWINDOWSDLL_API __declspec(dllimport)

With theses expanded macros, it can export functions. Every headers and bodys need to be decorated. Here is the explaination. When you build the library, you define de constant LMDBWINDOWSDLL_EXPORTS in the project settings then every macros instruct the compiler to export the functions. When you build an application who needs the dll, the macro is expanded as an import operation and at the link phase, you need to link with the little stub to attach the library.

If you are new to Windows DLLs making, check this portion of doc on MSDN: https://msdn.microsoft.com/en-us/library/a90k134d.aspx