How to debug boost::beast with mitmproxy

mitmproxy is a valuable tool to debug HTTPS request made using boost::beast.

In order to use it, remember that mitmproxy listents to localhost on port 8080 and forwards requests based on the HTTP Host header.

First, you need to disable SSL certificate verification:

beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
stream.set_verify_mode(ssl::verify_none);

Now, instead of connecting to an IP address based on DNS results such as

ip::tcp::resolver resolver(ioc);
get_lowest_layer(stream).connect(resolver.resolve({endpoint, "https"}));

directly connect to the 127.0.0.1:8080 endpoint:

boost::asio::ip::tcp::endpoint ep(
    boost::asio::ip::address::from_string("127.0.0.1"),
    8080
);
get_lowest_layer(stream).connect(ep);

Besides that, you can do anything as usual.