Internet Protocol
Header Only Library

HTTP Client Openssl

How to use HTTP client class with openssl

References

#include "ip.hpp"
#include "ip/http/httpclient.hpp"
#define ENABLE_SSL

Syntax

namespace internetprotocol
class http_client_ssl_c
#include <iostream>
#include <fstream>
#define ENABLE_SSL
#include <ip.hpp>

using namespace internetprotocol;

std::string loadfile(const std::string& file) {
    std::ifstream arquivo(file);

    if (!arquivo.is_open()) {
        throw std::runtime_error("Erro trying to open file: " + file);
    }

    std::stringstream buffer;
    buffer << arquivo.rdbuf();

    return buffer.str();
}
// Relative path to the files
const std::string cert = loadfile("cert.pem");
const std::string key = loadfile("key.pem");
const std::string csr = loadfile("csr.pem");
const std::string ca_cert = loadfile("ca-cert.pem");

int main()
{
    http_client_ssl_c net({ key, cert, "", "", file_format_e::pem, none, "" });
    
    http_request_t req;
    net.get("/", [&](const http_request_t &request, const std::shared_ptr<http_remote_c> &response) {
        http_response_t &res = response->headers();
        res.body = "hello, world!";
        response->write([](const asio::error_code &ec, const size_t bytes_sent) {
            // Check if response has been sent
        });
    });

    std::string input;
    while (std::getline(std::cin, input)) {
        if (input == "quit") {
            net.close();
            break;
        }
    }
    join_threads();

    return 0;
}

On this page