Sup folks, I have no clue why the binance server is responding with error 400 when using boost::beast library. I tried the same http request using openssl and it works fine. This is the error im getting in console:
HTTP/1.1 400 Bad RequestServer: CloudFrontDate: Wed, 18 Sep 2024 15:26:48 GMTContent-Type: text/htmlContent-Length: 915Connection: closeX-Cache: Error from cloudfrontVia: 1.1 c855d201fddbb6ef22989607fe8f5d1e.cloudfront.net (CloudFront)X-Amz-Cf-Pop: VIE50-C2X-Amz-Cf-Id: ckbq9NokUYRiAxcUFAfcYYG7CaE6v9xRx1sqlstwSuODrpbHGrA7Hg==<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><TITLE>ERROR: The request could not be satisfied</TITLE></HEAD><BODY><H1>400 ERROR</H1><H2>The request could not be satisfied.</H2><HR noshade size="1px">Bad request.We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.<BR clear="all">If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.<BR clear="all"><HR noshade size="1px"><PRE>Generated by cloudfront (CloudFront)Request ID: ckbq9NokUYRiAxcUFAfcYYG7CaE6v9xRx1sqlstwSuODrpbHGrA7Hg==</PRE><ADDRESS></ADDRESS></BODY></HTML>
And here is the code I use:
#include <boost/beast.hpp>#include <boost/asio.hpp>#include <iostream>#include <string>#include <cstdlib>namespace beast = boost::beast; namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp;void get_perpetual_btc_price() { try { // Set up the I/O context and stream net::io_context ioc; tcp::resolver resolver{ioc}; beast::tcp_stream stream{ioc}; // Resolve and connect auto const results = resolver.resolve("api.binance.com", "443"); stream.connect(results); // Prepare the HTTP GET request http::request<http::empty_body> req{http::verb::get, "/api/v3/ticker/price?symbol=BTCUSDT", 11}; req.set(http::field::host, "api.binance.com"); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); // Send the request http::write(stream, req); // Prepare buffer and response object beast::flat_buffer buffer; http::response<http::dynamic_body> res; beast::error_code ec; // Read the response http::read(stream, buffer, res, ec); if (ec) { throw beast::system_error{ec}; } // Output the response std::cout << res << std::endl; // Gracefully close the stream beast::error_code shutdown_ec; stream.socket().shutdown(tcp::socket::shutdown_both, shutdown_ec); if (shutdown_ec && shutdown_ec != beast::errc::not_connected) { throw beast::system_error{shutdown_ec}; } } catch (std::exception const& e) { std::cerr << "Error: " << e.what() << std::endl; }}int main() { get_perpetual_btc_price(); return 0;}
I tried to somehow alter the code and I even tried to setup custom server to accept connections and see the http message the client is sending and the raw http request looked right. However when I use this code, Im getting error 400 every time. Does anybody know what is the issue?