c++ - Error in getUpdates request to a Telegram bot using boost beast - Stack Overflow

admin2025-04-25  2

Trying to get updates from a telegram bot with a post request: POST ;token>/getUpdates

This works fine in Postman, I am getting the response from bot:

{
    "ok": true,
    "result": [
        {
            "update_id": 22720266,
            "message": {
                "message_id": 25,
                "from": {
                    "id": 67825600,
                    "is_bot": false,
... (etc) ...

But when I try to send the same request from my c++ boost http client, I get the following error:

HTTP/1.1 400 Bad Request

Server: nginx/1.18.0
Date: Thu, 16 Jan 2025 12:52:52 GMT
Content-Type: text/html
Content-Length: 157
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

I tried a few methods for sending the request, mainly from boost examples like .cpp

and

.cpp

Here is the code that I modified for the http (non-ssl) version:

int main(int argc, char** argv)
{
    //POST ;..token..>/getUpdates
    auto const host = ";;
    auto const port = "80";
    auto const target = "/bot<..token..>/getUpdates";

    int version = 11;

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

    // Launch the asynchronous operation
    std::make_shared<session>(ioc)->run(host, port, target, version);

    // Run the I/O service. The call will return when
    // the get operation is complete.
    ioc.run();

    return EXIT_SUCCESS;
}

And forming the request:

// Start the asynchronous operation
void run(char const* host, char const* port, char const* target, int version) {
        // Set up an HTTP POST:
        req_.version(version);
        req_.method(http::verb::post);
        req_.target(target);
        req_.set(http::field::host, host);
        req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
        req_.set(http::field::content_type, "application/json");
        req_.content_length(0);

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

For the ssl version I am doing pretty much the same thing, except for using port 443. The handshake completes fine, stream.handshake(ssl::stream_base::client); but then I get the same response with "bad request":

HTTP/1.1 400 Bad Request

Server: nginx/1.18.0

Thank you for any tips on how to fix this!

p.s. Additional info: cout of the https request that I am sending with boost:

-> Sending Request:
POST /bot7451143435:AAHFx0dLp6sB2ifaDNQRnUcMwKGtQdZEgio/getUpdates HTTP/1.1
Host: 
User-Agent: Boost.Beast/322

 === Request sent ===

(response: )
HTTP/1.1 400 Bad Request

Server: nginx/1.18.0
...
转载请注明原文地址:http://anycun.com/QandA/1745528214a90795.html