Internet Protocol
Header Only Library

HTTP Client Openssl

How to use HTTP client class with openssl

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class HttpClientSsl
#include <iostream>
#define ASIO_USE_OPENSSL
#include <IP/HTTP/HTTPClient.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    HttpClientSsl client;
    client.set_host("localhost", "3000");
    client.set_request_method();
    client.on_request_complete = [&](const Client::FResponse response) {
        std::cout << response.content << std::endl;
    };
    client.get_ssl_context().set_options(asio::ssl::verify_none);
    client.load_private_key_file("<key file>.pem"); //Relative path to your PEM file
    client.load_certificate_file("<cert file>.pem"); //Relative path to your PEM file
    client.update_ssl_socket();
    client.prepare_payload();
    client.process_request();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            break;
        }
    }
 
    return 0;
}

Functions

This class has the same functions from HttpClient, except for the ones shown below

Get Ssl Context

  • Set Ssl extra configuration.
  asio::ssl::context &get_ssl_context()

Get Ssl Socket

  • Get Ssl socket
  const asio::ssl::stream<asio::ip::tcp::socket> &get_ssl_socket() const

Update Ssl Socket

  • Update Ssl socket configuration
  • Must be called afer change options using get_ssl_context() function
  void update_ssl_socket()

Load Private Key Data

  • Load the private key from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_private_key_data(const std::string &key_data)

Load Private Key File

  • Load the private key from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_private_key_file(const std::string &filename)

Load Certificate Data

  • Load the certificate from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_data(const std::string &cert_data)

Load Certificate File

  • Load the certificate from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_file(const std::string &filename)

Load Certificate Chain Data

  • Load the certificate chain from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_chain_data(const std::string &cert_chain_data)

Load Certificate Chain File

  • Load the certificate chain from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_chain_file(const std::string &filename)

Load Verify File

  • Load the certificate from file and verify if it is valid.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_verify_file(const std::string &filename)

On this page