Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed | Gopher

C++ set up cpp-httplib with SSL support with cMake

Published: 14-12-2020 | Author: Remy van Elst | Text only version of this article


Table of Contents


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

If you like this article, consider sponsoring me by trying out a Digital Ocean VPS. With this link you'll get $100 credit for 60 days). (referral link)

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 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
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL>
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto>)

target_compile_definitions(${PROJECT_NAME} PUBLIC
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>: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

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
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL>
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto>)

target_compile_definitions(${PROJECT_NAME} PUBLIC
        $<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>: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
}
Tags: c++ , cmake , cpp , cpp-httplib , development , http , https , openssl , ssl , tutorials , web