C++ REST SDK (also known as Casablanca) offers support for HTTP client/server, JSON, URI, asynchronous streams, WebSockets client, oAuth and PPL Tasks.
We can get C++ REST SDK from CodePlex or use the one shipped with Visual Studio 2013.
This article gives suggestions of how to set up a Visual C++ project (particularly one that uses MFC) in order to be able to deal with Casablanca library. It also shows some simple sample code and a demo MFC project.
Set up the project for using C++ REST SDK
First, must find the location where C++ REST SDK is installed. With Visual Studio 2013 installed, I found it in the following path:
C:\Program Files (x86)\Microsoft SDKs\Cpp REST SDK for Visual Studio 2013\SDK\ which is similar to $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK.
Next, create a new MFC project or open an existing one, right click on it’s name, choose “Properties” from the context menu, then do the following:
- Choose All configurations in the Configuration combo box.
- Go to Configuration Properties / C/C++ / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\include to Additional Include Directories list.
- Go to Configuration Properties / C/C++ / Command Line and add -Zm200 in Additional Options field; this is necessary to avoid C3859 compiler error when include Casablanca headers in stdafx.h.
- Go to Configuration Properties / Linker / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\lib\$(PlatformTarget)\$(Configuration) to Additional Library Directories list.
- Go to Configuration Properties / Linker / Input and add casablanca120.lib to Additional Dependencies.
- Also in Configuration Properties / Linker / Input add casablanca120.dll to Delay Loaded DLLs list; this is necessary to avoid false memory leaks reported by the MFC framework;
- Go to Configuration Properties / Build Events / Post-Build Event and add the following command to Command Line field
copy /Y “$(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\bin\$(PlatformTarget)\$(Configuration)\casablanca120.dll” “$(OutDir)”; this copies the necessary Casablanca DLL from its install folder in the output directory of our project. - Close the project property pages dialog.
- In stdafx.h include all necesary Casablanca headers, e.g. http_client.h, json.h, pplxtasks.h and so on.
Now, we are ready to go on and use C++ REST SDK stuff in our own MFC application.
Notes
- Remember that other versions of Visual Studio may install C++ REST SDK in other location!
- May be other methods to set up the project paths, as for example by using Property Manager and/or edit .props files; I just presented one presumed simpler.
- Ones would prefer to get C++ REST SDK from CodePlex; probably, I’ll describe this alternative in a future article.
Next is some code sample. You can notice it uses lambda expressions and for that reason I included this article in Using Lambdas in MFC Applications series.
Using lambdas and C++ REST SDK with MFC – sample code
This sample sends a JSON structure in URI query part to a site which gives in response also a JSON which says if the sent one is valid or not and gives an error description if necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void CJsonVerifierDlg::OnClickButtonVerifyJson() { UpdateData(); try { auto task = _VerifyJsonTask(); task.wait(); auto spJsonValue = task.get(); if (spJsonValue) _DisplayResponse(spJsonValue); } catch (std::exception& e) { CString strMessage; strMessage.Format(L"Exception: %S", e.what()); AfxMessageBox(strMessage, MB_ICONERROR); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
pplx::task<std::shared_ptr<web::json::value>> CJsonVerifierDlg::_VerifyJsonTask() { return pplx::create_task([this]() { // base URI auto sBaseURI = utility::string_t(m_strBaseURI.GetString()); // build request URI auto sRequestURI = web::http::uri_builder() .append_query(L"json", m_strInputJson.GetString()).to_string(); // create http client web::http::client::http_client httpClient(sBaseURI); // create GET http request web::http::http_request httpRequest(U("GET")); //web::http::methods::GET); // set request URI httpRequest.set_request_uri(sRequestURI); // send request return httpClient.request(httpRequest); // then receive the response }).then([](web::http::http_response httpResponse) { std::shared_ptr<web::json::value> spJsonValue; if (web::http::status_codes::OK == httpResponse.status_code()) { spJsonValue = std::make_shared<web::json::value>(httpResponse.extract_json().get()); } return spJsonValue; }); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void CJsonVerifierDlg::_DisplayResponse(const std::shared_ptr<web::json::value>& spJsonValue) { if ((*spJsonValue)[U("validate")].as_bool()) { m_strIsValid = _T("YES"); m_nParseTime = (*spJsonValue)[U("parse_time_nanoseconds")].as_integer(); } else { m_strIsValid = _T("NO"); m_strError = (*spJsonValue)[U("error")].as_string().c_str(); } UpdateData(FALSE); } |
More details about project settings and source code can be found in the attached demo project.
Using lambdas and C++ REST SDK with MFC – demo project
Download: JSON Verifier - Demo Application using Casablanca.zip (1229)
References and related articles
- CodePlex: C++ REST SDK
- MSDN: C++ REST SDK (Codename “Casablanca”)
- Marius Bancila’s Blog: C++ REST SDK in Visual Studio 2013
- Visual C++ Team Blog: The C++ REST SDK (“Casablanca”)
- Dr. Dobb’s: Using the Microsoft C++ REST SDK
- Codexpert Blog: Using Lambdas in MFC Applications – Part 1: Sorting Arrays
- Codexpert Blog: Using Lambdas in MFC Applications – Part 2: Replacing Callback Functions
- Codexpert Blog: C++11: Let’s Write a “Hello Lambda!”
Hi, I was studying this example and found it very interesting.
I was working in some modifications where I use STL and I get this linker error:
Error 1 error LNK1194: cannot delay-load ‘casablanca120.dll’ due to import of data symbol ‘”__declspec(dllimport) public: static class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > const web::http::methods::GET” (__imp_?GET@methods@http@web@@2V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@B)’; link without /DELAYLOAD:casablanca120.dll D:\dev\src\C++\Demo Casablanca\JsonVerifier\LINK JsonVerifier
I just deleted the delayload, but I’m courious about this. Thanks for the article.
Edgar