프로그래밍 팁

boost beast http client post form

바보 악마 2019. 10. 17. 14:15

        auto UrlEncode = [](const std::string& str)
        {
             std::string ret;
             for (size_t i = 0; i < str.length(); ++i)
            {
                if (0 != std::isalnum(str[i]))
                 {
                     ret += str[i];
                 }
                 else
                 {
                     ret += '%';
                     ret += fmt::format("{:2X}", static_cast<byte>(str[i]));
                 }
             }

            return ret;
        };

        try
        {
            namespace beast = boost::beast;     // from <boost/beast.hpp>
            namespace http = beast::http;       // from <boost/beast/http.hpp>
            namespace net = boost::asio;        // from <boost/asio.hpp>
            using tcp = net::ip::tcp;           // from <boost/asio/ip/tcp.hpp>



            auto const host = "www.zzz.co.kr";
            auto const port = "http";
            auto const target = "/api/logs";
            constexpr int version = 11;

            // The io_context is required for all I/O
            net::io_context ioc;

            // These objects perform our I/O
            tcp::resolver resolver(ioc);
            beast::tcp_stream stream(ioc);

            // Look up the domain name
            auto const results = resolver.resolve(host, port);

            // Make the connection on the IP address we get from a lookup
            stream.connect(results);


            // Set up an HTTP GET request message
            http::request<http::string_body> req{ http::verb::post, target, version };
            req.set(http::field::host, host);
            req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
           
           
            std::string body = UrlEncode("id1") + "=" + UrlEncode("content1");
             body += "&" + UrlEncode("id2") + "=" + UrlEncode("content2");
             body += "&" + UrlEncode("id3") + "=" + UrlEncode("content3");
             body += "&" + UrlEncode("id4") + "=" + UrlEncode("content4");
           
             req.body() = body;
             req.set(http::field::content_type, "application/x-www-form-urlencoded;charset=UTF-8");
             req.set(http::field::content_length, body.length());


            // Send the HTTP request to the remote host
            http::write(stream, req);

            // This buffer is used for reading and must be persisted
            beast::flat_buffer buffer;

            // Declare a container to hold the response
            http::response<http::dynamic_body> res;

            // Receive the HTTP response
            http::read(stream, buffer, res);

            // Write the message to standard out
            std::cout << res << std::endl;

            // Gracefully close the socket
            beast::error_code ec;
            stream.socket().shutdown(tcp::socket::shutdown_both, ec);

            // not_connected happens sometimes
            // so don't bother reporting it.
            //
            if (ec && ec != beast::errc::not_connected)
                throw beast::system_error{ ec };

            // If we get here then the connection is closed gracefully
        }
        catch (std::exception const& e)
        {
            std::cerr << "Error: " << e.what() << std::endl;
            return;
        }

'프로그래밍 팁' 카테고리의 다른 글

c++ DP  (0) 2020.12.28
c++ basic  (0) 2020.12.04
boost asio http client post form  (0) 2019.10.15
CMD ERRORLEVEL & ^  (0) 2019.04.19
GPGS  (0) 2019.04.15