1//
2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10//------------------------------------------------------------------------------
11//
12// Example: HTTP client, synchronous for every method on httpbin
13//
14//------------------------------------------------------------------------------
15
16//[example_http_client_methods
17
18#include <boost/beast/core.hpp>
19#include <boost/beast/http.hpp>
20#include <boost/beast/version.hpp>
21#include <boost/asio/connect.hpp>
22#include <boost/asio/ip/tcp.hpp>
23#include <cstdlib>
24#include <iostream>
25#include <string>
26
27namespace beast = boost::beast; // from <boost/beast.hpp>
28namespace http = beast::http; // from <boost/beast/http.hpp>
29namespace net = boost::asio; // from <boost/asio.hpp>
30using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
31
32
33// perform a get request
34void do_get(beast::tcp_stream & stream,
35 http::request<http::string_body> & req,
36 beast::flat_buffer buffer,
37 http::response<http::dynamic_body> & res)
38{
39 req.target(s: "/get");
40 req.method(v: beast::http::verb::get);
41 http::write(stream, msg: req);
42 http::read(stream, buffer, msg&: res);
43}
44
45// perform a head request
46void do_head(beast::tcp_stream & stream,
47 http::request<http::string_body> & req,
48 beast::flat_buffer buffer,
49 http::response<http::dynamic_body> & res)
50{
51 // we reuse the get endpoint
52 req.target(s: "/get");
53 req.method(v: beast::http::verb::head);
54 http::write(stream, msg: req);
55
56 /* the head response will send back a content-length
57 * without a body. The other requests don't set content-length when not
58 * sending a body back.
59 *
60 * the response parser doesn't know that we sent head,
61 * so we need to manually make sure we're only reading the header
62 * otherwise we're waiting forever for data.
63 */
64
65 http::response_parser<http::dynamic_body> p;
66 http::read_header(stream, buffer, parser&: p);
67 // move the result over
68 res = p.release();
69}
70
71
72// perform a patch request
73void do_patch(beast::tcp_stream & stream,
74 http::request<http::string_body> & req,
75 beast::flat_buffer buffer,
76 http::response<http::dynamic_body> & res)
77{
78 req.target(s: "/patch");
79 req.method(v: beast::http::verb::patch);
80 req.body() = "Some random patch data";
81 req.prepare_payload(); // set content-length based on the body
82 http::write(stream, msg: req);
83 http::read(stream, buffer, msg&: res);
84}
85
86
87// perform a put request
88void do_put(beast::tcp_stream & stream,
89 http::request<http::string_body> & req,
90 beast::flat_buffer buffer,
91 http::response<http::dynamic_body> & res)
92{
93 req.target(s: "/put");
94 req.method(v: beast::http::verb::put);
95 req.body() = "Some random put data";
96 req.prepare_payload(); // set content-length based on the body
97 http::write(stream, msg: req);
98 http::read(stream, buffer, msg&: res);
99}
100
101// perform a post request
102void do_post(beast::tcp_stream & stream,
103 http::request<http::string_body> & req,
104 beast::flat_buffer buffer,
105 http::response<http::dynamic_body> & res)
106{
107 req.target(s: "/post");
108 req.method(v: beast::http::verb::post);
109 req.body() = "Some random post data";
110 req.prepare_payload(); // set content-length based on the body
111 http::write(stream, msg: req);
112 http::read(stream, buffer, msg&: res);
113}
114
115
116// perform a delete request
117void do_delete(beast::tcp_stream & stream,
118 http::request<http::string_body> & req,
119 beast::flat_buffer buffer,
120 http::response<http::dynamic_body> & res)
121{
122 req.target(s: "/delete");
123 req.method(v: beast::http::verb::delete_);
124 // NOTE: delete doesn't require a body
125 req.body() = "Some random delete data";
126 req.prepare_payload(); // set content-length based on the body
127 http::write(stream, msg: req);
128 http::read(stream, buffer, msg&: res);
129}
130
131
132// Performs an HTTP request against httpbin.cpp.al and prints request & response
133int main(int argc, char** argv)
134{
135 try
136 {
137 // Check command line arguments.
138 if(argc != 2)
139 {
140 std::cerr <<
141 "Usage: http-client-method <method> \n" <<
142 "Example:\n" <<
143 " http-client-method get\n" <<
144 " http-client-method post\n";
145 return EXIT_FAILURE;
146 }
147
148 for (char * c = argv[1]; *c != '\0'; c++)
149 *c = static_cast<char>(std::tolower(c: *c));
150 beast::string_view method{argv[1]};
151
152 // The io_context is required for all I/O
153 net::io_context ioc;
154
155 // These objects perform our I/O
156 tcp::resolver resolver(ioc);
157 beast::tcp_stream stream(ioc);
158
159 // Look up the domain name
160 auto const results = resolver.resolve(host: "httpbin.cpp.al", service: "http");
161
162 // Make the connection on the IP address we get from a lookup
163 stream.connect(endpoints: results);
164
165
166 // Set up an HTTP GET request message
167 http::request<http::string_body> req;
168 req.set(name: http::field::host, value: "httpbin.cpp.al");
169 req.set(name: http::field::user_agent, BOOST_BEAST_VERSION_STRING);
170
171
172 // This buffer is used for reading and must be persisted
173 beast::flat_buffer buffer;
174
175 // Declare a container to hold the response
176 http::response<http::dynamic_body> res;
177
178 if (method == "get")
179 do_get(stream, req, buffer, res);
180 else if (method == "head")
181 do_head(stream, req, buffer, res);
182 else if (method == "patch")
183 do_patch(stream, req, buffer, res);
184 else if (method == "put")
185 do_put(stream, req, buffer, res);
186 else if (method == "post")
187 do_post(stream, req, buffer, res);
188 else if (method == "delete")
189 do_delete(stream, req, buffer, res);
190 else
191 {
192 std::cerr << "Unknown method: " << method << std::endl;
193 return EXIT_FAILURE;
194 }
195
196 // Write the message to standard out
197 std::cout << "Request send:\n-----------------------------\n"
198 << req << std::endl;
199
200
201 // Write the message to standard out
202 std::cout << "\n\nResponse received:\n-----------------------------\n"
203 << res << std::endl;
204
205 // Gracefully close the socket
206 beast::error_code ec;
207 stream.socket().shutdown(what: tcp::socket::shutdown_both, ec);
208
209 // not_connected happens sometimes
210 // so don't bother reporting it.
211 //
212 if(ec && ec != beast::errc::not_connected)
213 throw beast::system_error{ec};
214
215 // If we get here then the connection is closed gracefully
216 }
217 catch(std::exception const& e)
218 {
219 std::cerr << "Error: " << e.what() << std::endl;
220 return EXIT_FAILURE;
221 }
222 return EXIT_SUCCESS;
223}
224
225//]
226

source code of boost/libs/beast/example/http/client/methods/http_client_methods.cpp