Internet Protocol
Header Only Library

HTTP Client

How to use HTTP client class

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class HttpClient
#include <iostream>
#include <IP/HTTP/HTTPClient.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    HttpClient 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.prepare_payload();
    client.process_request();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            break;
        }
    }
 
    return 0;
}

Variables

EVerb

namespace InternetProtocol
 
enum class EVerb : uint8_t {
    DEL = 0,
    GET = 1,
    HEAD = 2,
    OPTIONS = 3,
    PATCH = 4,
    POST = 5,
    PUT = 6,
    TRACE = 7,
}

FRequest

namespace InternetProtocol::Client
 
struct FRequest {
    std::map<std::string, std::string> params;
    EVerb verb = EVerb::GET;
    std::string path = "/";
    std::string version = "1.1";
    std::map<std::string, std::string> headers;
    std::string body;
 
};

FResponse

namespace InternetProtocol::Client
 
struct FResponse {
    std::map<std::string, std::vector<std::string> > headers;
    int contentLenght = 0;
    std::string content;
};

Functions

Set Host

  • Set the adress and port of host
void set_host(const std::string &url = "localhost", const std::string &port = "3000")

Get Socket

  • Get socket connection properties
asio::ip::tcp::socket &get_socket()

Set Request

  • Set the HTTP request to be sent
void set_request(const Client::FRequest &value)

Get Request

  • Get the HTTP request to be sent
Client::FRequest get_request() const

Set Request Method

  • Set request verb
void set_request_method(EMethod requestMethod = EMethod::GET)

Get Request Method

  • Get request verb
EMethod get_request_method() const

Set Version

  • Set request version
void set_version(const std::string &version = "1.1)

Get Version

  • Get request version
std::string get_version() const

Set Path

  • Set request version
void set_path(const std::string &path = "/")

Get Path

  • Get request version
std::string get_path() const

Set Params

  • Set request parameters
void set_headers(const std::map<std::string, std::string> &headers)

Get Params

  • Return a reference of request parameters
  • Use this function to modify parameters
std::map<std::string, std::string> &get_params()

Set Headers

  • Set request headers
void set_headers(const std::map<std::string, std::string> &headers)

Get Headers

  • Return a reference if request headers
  • Use this function to modify or append values to headers
std::map<std::string, std::string> &get_headers()

Set Body

  • Set request body
void set_body(const std::string &value)

Get Body

  • Return a reference to request body
  • Use this function to modify or append value to body
std::string &get_body()

Get Request Data

  • Get request data
Client::FRequest get_request_data() const

Prepare Payload

  • Prepare request data
  • Must be called before call processRequest function
void prepare_payload()

Async Prepare Payload

  • Prepare request data asynchronously
  • Must be called before call processRequest function
void async_prepare_payload()

Get Payload Data

  • Get processed payload data
std::string get_payload_data() const

Get Response Data

  • Get latest response data
FResponse get_response_data() const

Process Request

  • Send request
  • Return false if payload is empty
bool process_request()

Close

  • Close connection or cancel request
void close()

Clear Request

  • Clear request data
void clear_request()

Clear Payload

  • Clear payload data
void clear_payload()

Clear Response

  • Clear response buffer
void clear_response()

Get Error Code

  • Get latest error code
asio::error_code get_error_code() const

Events

On Async Payload Finished

  • Event triggered when payload process has been finished
std::function<void()> on_async_payload_finished

On Request Progress

  • Event triggered while the request is in progress
  • size_t: bytes sent
  • size_t: bytes received
std::function<void(const size_t, const size_t)> on_request_progress

On Request Complete

  • Event triggered when request is done
  • FResponse: Response struct
std::function<void(const Client::FResponse)> on_request_complete

On Request Fail

  • Event triggered when request fail
  • FResponse: Response struct
std::function<void(const Client::FResponse)> on_request_fail

On Close

  • Event triggered when socket is closed
std::function<void()> on_close

On Error

  • Event triggered if any error occur during async process
  • const asio::error_code &: if error occur, error code will be different from 0
  • Normally, when an error occurs, the socket is automatically closed, triggering the on_close event.
std::function<void(const asio::error_code &)> on_error