Boost.beast : 如何 return json 仅响应

Boost.beast : how to return json response only

代码:https://gist.github.com/Naseefabu/173c928603e564879683ccdf10d9d0f8

当我 运行 执行此操作并将响应打印到控制台时:

Sending GET /api/v3/time HTTP/1.1
Host: api.binance.com
content-type: application/json
User-Agent: Boost.Beast/330


HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 28
Connection: keep-alive
Date: Fri, 13 May 2022 17:18:47 GMT
Server: nginx
x-mbx-uuid: 7e7465db-011a-4308-aa1e-6603d72c8c9a
x-mbx-used-weight: 1
x-mbx-used-weight-1m: 1
Strict-Transport-Security: max-age=31536000; includeSubdomains
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'self'
X-Content-Security-Policy: default-src 'self'
X-WebKit-CSP: default-src 'self'
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, HEAD, OPTIONS
X-Cache: Miss from cloudfront
Via: 1.1 dfccb338f8c0489ab09835ea7dbad1a8.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: MAA51-P1
X-Amz-Cf-Id: waTqtDHSDpk74QB7zkF5Ya0CdRVWuJuC-M4TZqSuMd2bfXawkq6o6g==

{"serverTime":1652462327804}
shutdown: stream truncated

如果我只想得到 json 响应怎么办:{"serverTime":1652462327804}? 并存储在 json 变量中,以便我可以根据需要使用它,其他信息对我来说并不那么重要,提前谢谢!

 binapi::AsyncRest::httpClient* client;

这非常可疑,因为 class 使用的是 enable_shared_from_this()。很确定应该是

auto client =
    std::make_shared<binapi::AsyncRest::httpClient>(ioc.get_executor(), ctx);

接下来,我假设 get_server_time 是一个静态函数。我不明白为什么它是 httpClient.

的成员

因此将所有缺失的代码重新添加(使用大量经验):

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/url.hpp>
#include <boost/url/src.hpp> // for header-only
#include <fstream>
#include <iomanip>
#include <iostream>
namespace net   = boost::asio;
namespace beast = boost::beast;
namespace http  = boost::beast::http;
namespace ssl   = boost::asio::ssl;
using net::ip::tcp;

namespace binapi { namespace AsyncRest {

    // Report a failure
    void fail_http(beast::error_code ec, char const* what) {
        std::cerr << what << ": " << ec.message() << "\n";
    }

    struct httpClient : std::enable_shared_from_this<httpClient> {
        using executor = net::any_io_executor;
        using Stream   = beast::ssl_stream<beast::tcp_stream>;

        tcp::resolver                     resolver_;
        Stream                            stream_;
        beast::flat_buffer                buffer_;
        http::request<http::empty_body>   req_;
        http::response<http::string_body> res_;

        httpClient(executor ex, ssl::context& ctx);

        // Start the asynchronous operation
        void run(boost::url, http::verb);
        void on_resolve(beast::error_code, tcp::resolver::results_type);
        void on_connect(beast::error_code, tcp::resolver::results_type::endpoint_type);
        void on_handshake(beast::error_code);
        void on_write(beast::error_code, size_t bytes_transferred);
        void on_read(beast::error_code, size_t bytes_transferred);
        void on_shutdown(beast::error_code);
    };

    httpClient::httpClient(executor ex, ssl::context& ctx)
        : resolver_(ex)
        , stream_(ex, ctx) {}

    // Start the asynchronous operation
    void httpClient::run(boost::url url, http::verb action) {

        std::string const host(url.host());
        std::string const service = url.has_port() //
            ? url.port()
            : (url.scheme_id() == boost::urls::scheme::https) //
                ? "https"
                : "http";
        url.remove_origin(); // becomes req_.target()

        // Set SNI Hostname (many hosts need this to handshake
        // successfully)
        if (!SSL_set_tlsext_host_name(stream_.native_handle(), host.c_str())) {
            beast::error_code ec{static_cast<int>(::ERR_get_error()),
                                 net::error::get_ssl_category()};
            std::cerr << ec.message() << "\n";
            return;
        }

        // Set up an HTTP GET/POST/DELETE/PUT request message
        // req_.version(version);
        req_.method(action);
        req_.target(url.c_str());
        req_.set(http::field::host, host);
        req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
        req_.prepare_payload(); // make HTTP 1.1 compliant

        // Look up the domain name
        resolver_.async_resolve(
            host, service,
            beast::bind_front_handler(&httpClient::on_resolve, shared_from_this()));
    }

    void httpClient::on_resolve(beast::error_code           ec,
                                tcp::resolver::results_type results) {
        if (ec)
            return fail_http(ec, "resolve");

        // Set a timeout on the operation
        beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));

        // Make the connection on the IP address we get from a lookup
        beast::get_lowest_layer(stream_).async_connect(
            results,
            beast::bind_front_handler(&httpClient::on_connect, shared_from_this()));
    }

    void httpClient::on_connect(beast::error_code ec,
                                tcp::resolver::results_type::endpoint_type) {
        if (ec)
            return fail_http(ec, "connect");

        // Perform the SSL handshake
        stream_.async_handshake(
            ssl::stream_base::client,
            beast::bind_front_handler(&httpClient::on_handshake, shared_from_this()));
    }

    void httpClient::on_handshake(beast::error_code ec) {
        if (ec)
            return fail_http(ec, "handshake");

        // Set a timeout on the operation
        beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));

        // Send the HTTP request to the remote host
        std::cout << "Sending " << req_ << std::endl;
        http::async_write(
            stream_, req_,
            beast::bind_front_handler(&httpClient::on_write, shared_from_this()));
    }

    void httpClient::on_write(beast::error_code ec, size_t bytes_transferred) {
        boost::ignore_unused(bytes_transferred);

        if (ec)
            return fail_http(ec, "write");

        // Receive the HTTP response
        http::async_read(
            stream_, buffer_, res_,
            beast::bind_front_handler(&httpClient::on_read, shared_from_this()));
    }

    void httpClient::on_read(beast::error_code ec, size_t bytes_transferred) {
        boost::ignore_unused(bytes_transferred);

        if (ec)
            return fail_http(ec, "read");

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

        // Set a timeout on the operation
        beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));

        // Gracefully close the stream
        stream_.async_shutdown(
            beast::bind_front_handler(&httpClient::on_shutdown, shared_from_this()));
    }

    void httpClient::on_shutdown(beast::error_code ec) {
        if (ec == net::error::eof) {
            ec = {};
        }
        if (ec)
            return fail_http(ec, "shutdown");
    }

    static void get_server_time(net::io_context& ioc, ssl::context& ctx) {
        static boost::url_view const uri{"https://api.binance.com/api/v3/time"};

        std::make_shared<httpClient>(net::make_strand(ioc), ctx)
            ->run(uri, http::verb::get);
    }
}} // namespace binapi::AsyncRest

int main() {
    net::io_context                ioc;

    // The SSL context is required, and holds certificates
    ssl::context ctx{ssl::context::tlsv12_client};

    // Verify the remote server's certificate
    ctx.set_verify_mode(ssl::verify_peer);
    ctx.set_default_verify_paths();

    binapi::AsyncRest::get_server_time(ioc, ctx);

    ioc.run();
}

现在我们知道 res_beast::http::response<beast::http::string_body>。所以,如果你只想打印正文,打印:

    std::cout << res_.body() << std::endl;

版画

Sending GET /api/v3/time HTTP/1.1
Host: api.binance.com
User-Agent: Boost.Beast/330


{"serverTime":1652476115413}
shutdown: stream truncated

只打印时间:

    static constexpr boost::gregorian::date s_epoch{1970, 1, 1};

    auto epoch_seconds = json::parse(res_.body()).at("serverTime").as_int64();
    ptime serverTime(s_epoch, boost::posix_time::milliseconds(epoch_seconds));

    std::cout << serverTime << std::endl;

版画

2022-May-13 21:38:55.982000

Summary/Notes

你真正想要的是像

这样的函数
 ptime server_time();

或者

 void async_server_time(auto completionToken);

您可能希望 共享 客户端 class 实例,而不是每次调用都重新连接。