1#include "json_body.hpp"
2#include <boost/json/src.hpp>
3
4#include <boost/beast/core.hpp>
5#include <boost/beast/http.hpp>
6#include <boost/beast/version.hpp>
7#include <iostream>
8
9namespace beast = boost::beast; // from <boost/beast.hpp>
10namespace http = beast::http; // from <boost/beast/http.hpp>
11namespace net = boost::asio; // from <boost/asio.hpp>
12using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
13
14int main(int argc, char ** argv)
15{
16 // Our test endpoint for testing the json
17 const auto host = "postman-echo.com";
18 const auto target = "/post";
19
20 // The io_context is required for all I/O
21 net::io_context ioc;
22
23 // These objects perform our I/O
24 tcp::resolver resolver(ioc);
25 beast::tcp_stream stream(ioc);
26
27 // Look up the domain name
28 auto const results = resolver.resolve(host, service: "80");
29
30 // Make the connection on the IP address we get from a lookup
31 stream.connect(endpoints: results);
32
33 // Set up an HTTP POST request message
34 http::request<json_body> req{http::verb::post, target, 11};
35 req.set(name: http::field::host, value: host);
36 req.set(name: http::field::user_agent, BOOST_BEAST_VERSION_STRING);
37 req.set(name: http::field::content_type, value: "application/json");
38 req.body() = {{"type", "test"}, {"content", "pure awesomeness"}};
39 req.prepare_payload();
40 // Send the HTTP request to the remote host
41 http::write(stream, msg: req);
42
43 // This buffer is used for reading and must be persisted
44 beast::flat_buffer buffer;
45
46 // Get the response
47 http::response<json_body> res;
48
49 // Receive the HTTP response
50 http::read(stream, buffer, msg&: res);
51
52 // Write the message to standard out
53 std::cout << res << std::endl;
54
55 // Gracefully close the socket
56 beast::error_code ec;
57 stream.socket().shutdown(what: tcp::socket::shutdown_both, ec);
58
59
60 return 0;
61}
62

source code of boost/libs/beast/example/http/client/body/json_client.cpp