MCPcopy Create free account
hub / github.com/boostorg/beast / main

Function main

example/http/client/body/json_client.cpp:14–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
13
14int main(int, char **)
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, "80");
29
30 // Make the connection on the IP address we get from a lookup
31 stream.connect(results);
32
33 // Set up an HTTP POST request message
34 http::request<json_body> req{http::verb::post, target, 11};
35 req.set(http::field::host, host);
36 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
37 req.set(http::field::content_type, "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, 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, 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(tcp::socket::shutdown_both, ec);
58
59
60 return 0;
61}

Callers

nothing calls this directly

Calls 4

bodyMethod · 0.80
shutdownMethod · 0.80
writeFunction · 0.50
readFunction · 0.50

Tested by

no test coverage detected