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);

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 endpoint:

boost::asio::ip::tcp::endpoint ep(

Besides that, you can do anything as usual.