-
Notifications
You must be signed in to change notification settings - Fork 0
/
beast_https_get.cpp
76 lines (54 loc) · 2.25 KB
/
beast_https_get.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "libs/beast/example/common/root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <iostream>
#include <string>
#include "json.hpp"
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
nlohmann::json https_get_json(std::string const& host, std::string const& target)
{
auto const port = "443";
// The io_service is required for all I/O
boost::asio::io_service ios;
// The SSL context is required, and holds certificates
ssl::context ctx{ ssl::context::sslv23_client };
// This holds the root certificate used for verification
// load_root_certificates(ctx);
// These objects perform our I/O
tcp::resolver resolver{ ios };
ssl::stream<tcp::socket> stream{ ios, ctx };
// Look up the domain name
auto const lookup = resolver.resolve({ host, port });
// Make the connection on the IP address we get from a lookup
boost::asio::connect(stream.next_layer(), lookup);
// Perform the SSL handshake
stream.handshake(ssl::stream_base::client);
// Set up an HTTP GET request message
http::request<http::string_body> req{ http::verb::get, target, 11 };
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(stream, req);
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::string_body> res;
// Receive the HTTP response
http::read(stream, buffer, res);
// Gracefully close the stream
boost::system::error_code ec;
stream.shutdown(ec);
if (ec == boost::asio::error::eof)
ec = {};
if (ec)
throw boost::system::system_error{ ec };
// If we get here then the connection is closed gracefully
return nlohmann::json::parse(res.body());
}