This is a text-only version of the following page on https://raymii.org: --- Title : C++ set up cpp-httplib with SSL support with cMake Author : Remy van Elst Date : 14-12-2020 URL : https://raymii.org/s/tutorials/Cpp_set_up_cpp-httplib_with_ssl_support_with_cmake.md.html Format : Markdown/HTML --- For a small personal project that talks to a few JSON API's and does some data parsing I needed a header only C++ HTTP library. Header only because that is the simplest way to include it in my project, just copy and paste the file. I came across the project [cpp-httplib][1], which fits my needs, does all the http methods, small, a few examples and it looks modern and has recent development commits. Setup and getting it working was easy, but as soon as I tried an https url, I got an exception (` what(): 'https' scheme is not supported.`). This guide shows you how to setup cpp-httplib for SSL support with cmake. It took me a bit longer than I wanted to set it up correctly, so why not save you the effort.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

This is the example code we're compiling, as is in the readme, for HTTP: httplib::Client cli("http://jsonplaceholder.typicode.com"); if (auto res = cli.Get("/todos/1")) { if (res->status == 200) { std::cout << res->body << std::endl; } } Example output for HTTP: /home/remy/CLionProjects/example1/cmake-build-debug/example1 { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } For HTTPS you must read the entire article, at the bottom is the correct code and CmakeLists.txt, since just changing the URL will not work. ### OpenSSL support via cmake The [README][2] has one paragraph on SSL support, if you strip out the example, it's more like one line: > SSL support is available with CPPHTTPLIB OPENSSL SUPPORT. libssl and libcrypto should be linked. On Ubuntu you should install the OpenSSL development libraries: apt install libssl-dev For the rest I assume you have your development libraries installed and cmake set up. In your `CmakeLists.txt` you should add the following to add OpenSSL: find_package(OpenSSL REQUIRED) if(OPENSSL_FOUND) set(HTTPLIB_IS_USING_OPENSSL TRUE) endif() The `cpp-httplib` library also needs a few linker flags and compile options, which you can add to cmake with the below lines: target_link_libraries(${PROJECT_NAME} PUBLIC $<$:OpenSSL::SSL> $<$:OpenSSL::Crypto>) target_compile_definitions(${PROJECT_NAME} PUBLIC $<$:CPPHTTPLIB_OPENSSL_SUPPORT> ) The compile definitons are a complicated way of doing `-DCPPHTTPLIB_OPENSSL_SUPPORT`, based on an earlier variable. These two blocks should be everything you need for your code to be able to link agains OpenSSL. ### Hostname without the https part This took me another few minutes to find out. Each and every request I tried to do failed, with a `httplib::Connection` error: ![error][5] Nothing more, no specific SSL error, nothing. Turns out, this code doesn't work if you include the scheme part inside the URL (`https://`): httplib::SSLClient cli("https://jsonplaceholder.typicode.com"); If you omit that part, it will work: httplib::SSLClient cli("jsonplaceholder.typicode.com"); It doesn't help that there is no error message and that the `.Error()` method just says `Connection`. What is happening here, more than just "something wrong". Took me a few more tries and looking at the unit tests to figure out what the intended behaviour should be. You can use the preprocessor macro to check if you can use SSL, example below: #ifdef CPPHTTPLIB_OPENSSL_SUPPORT httplib::SSLClient cli(_domain); cli.enable_server_certificate_verification(true); #else httplib::Client cli(_domain); #endif Make sure to catch any exceptions and check for nullpointers. ### Conclusion & Full Code Setting up the correct cMake flags and troubleshooting why code that should work doesn't work took me more time than I wanted, so I hope this guide saves you some time. Below you'll find the full example code. CmakeLists.txt: cmake_minimum_required(VERSION 3.17) project(Example1) set(CMAKE_CXX_STANDARD 17) find_package(OpenSSL REQUIRED) if(OPENSSL_FOUND) set(HTTPLIB_IS_USING_OPENSSL TRUE) endif() add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PUBLIC $<$:OpenSSL::SSL> $<$:OpenSSL::Crypto>) target_compile_definitions(${PROJECT_NAME} PUBLIC $<$:CPPHTTPLIB_OPENSSL_SUPPORT> ) main.cpp int main() { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT httplib::SSLClient cli("jsonplaceholder.typicode.com"); cli.enable_server_certificate_verification(true); #else httplib::Client cli(http://jsonplaceholder.typicode.com); #endif if (auto res = cli.Get("/todos/1")) { if (res->status == 200) { std::cout << res->body << std::endl; } } return 0; } Output: /home/remy/CLionProjects/example1/cmake-build-debug/example1 { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } [1]: https://github.com/yhirose/cpp-httplib [2]: https://github.com/yhirose/cpp-httplib#openssl-support [5]: /s/inc/img/httplib-ssl.png --- License: All the text on this website is free as in freedom unless stated otherwise. This means you can use it in any way you want, you can copy it, change it the way you like and republish it, as long as you release the (modified) content under the same license to give others the same freedoms you've got and place my name and a link to this site with the article as source. This site uses Google Analytics for statistics and Google Adwords for advertisements. You are tracked and Google knows everything about you. Use an adblocker like ublock-origin if you don't want it. All the code on this website is licensed under the GNU GPL v3 license unless already licensed under a license which does not allows this form of licensing or if another license is stated on that page / in that software: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Just to be clear, the information on this website is for meant for educational purposes and you use it at your own risk. I do not take responsibility if you screw something up. Use common sense, do not 'rm -rf /' as root for example. If you have any questions then do not hesitate to contact me. See https://raymii.org/s/static/About.html for details.