Contents
HTTP client

Overview

HTTP and HTTPS clients that use the Boost Asio and Beast libraries.

The clients currently provide synchronous GET, HEAD, and POST calls.

Quick start

#include <Balau/Network/Http/Client/HttpClient.hpp>
#include <Balau/Network/Http/Client/HttpsClient.hpp>

Construction

In order to create a client, specify the host to the client constructor.

			// Create an HTTPS client with default SSL port 443.
			HttpsClient client("borasoftware.com");
		

The default user agent used in the client is "Balau <version>", where <version> is the version of the Balau library. The HTTP version in the above client is version 1.1.

If the required port, user agent and/or HTTP version are different to the default values, they can be specified in the constructor

			// Create an HTTP client with port 12345.
			HttpClient client1("example.com", 12345);

			// Create an HTTP client with port 12345 and user agent "Anon".
			HttpClient client2("example.com", 12345, "Anon");

			// Create an HTTP client with port 12345, user agent "Anon", and HTTP version 1.0.
			HttpClient client3("example.com", 12345, "Anon", "1.0");
		

If the scheme in the URL should determine whether an HTTP or HTTPS client should be created, the HttpClient::newClient static method can be used. In this API, the optional port number should also be included in the URL.

			// Create HTTP or HTTPS clients, according to the supplied URL.
			auto client4 = HttpClient::makeClient("https://borasoftware.com");
			auto client5 = HttpClient::makeClient("http://example.com:12345");
		

The HttpClient::newClient static method also accepts an optional user agent and HTTP version.

			// Create a client with a custom user agent.
			auto client6 = HttpClient::makeClient("https://borasoftware.com", "Anon");

			// Create a client with a custom user agent and HTTP version 1.0.
			auto client7 = HttpClient::makeClient("http://example.com:12345", "Anon", "1.0");
		

Usage

In order to perform synchronous GET, HEAD, and POST calls, the get, head, and post methods can be used.

			// Perform a GET request.
			CharVectorResponse response1 = client.get("/");

			// Perform a HEAD request.
			EmptyResponse response2 = client.head("/");

			// Perform a POST request.
			const std::string body = generateBody();
			CharVectorResponse response3 = client.post("/api/execute", body);
		

The GET and POST requests return a CharVectorResponse, which contains response headers and a character vector body. The HEAD request returns an EmptyResponse, which only contains response headers.