I’m working on a C++ Telegram bot project and need help with file uploads. Currently using a C++ Telegram bot library for development.
I understand that according to the Telegram Bot API documentation, files must be sent through POST requests using multipart/form-data format, similar to standard web browser file uploads.
I’m trying to figure out the proper C++ implementation for this. While I could use plain CURL, the library I’m using has a built-in HTTP client class called HttpClientWrapper that might be more suitable.
Here’s my current attempt:
TgBot::HttpClientWrapper httpClient;
std::ostringstream urlStream;
urlStream << "https://api.telegram.org/bot" << myBotToken;
urlStream << "/sendDocument";
TgBot::Url apiUrl(urlStream.str());
std::vector<TgBot::HttpReqArg> parameters;
parameters.emplace_back("chat_id", targetChatId);
std::string fileParam = "@";
fileParam += documentPath;
parameters.emplace_back("document", fileParam, true);
response = httpClient.makeRequest(apiUrl, parameters);
However, this returns an error:
“{"ok":false,"error_code":404,"description":"Not Found"}”
What’s the correct approach for sending files through Telegram’s API using C++?
That 404 error usually means there’s something wrong with your URL or how you’re handling the HTTP method. I’ve hit this exact issue before with TgBot-cpp. First, check your bot token - make sure there aren’t any extra spaces or weird characters. The URL format looks right, but test the token with a simple getMe request to be sure it’s valid. The file path might be your problem too. That “@” prefix works fine with curl, but you probably need to read the file content directly and pass it as binary data instead. Most HTTP wrappers want the actual file content, not just a path reference. Turn on debug logging in your HTTP client if you can. You’ll see exactly what request is being sent and whether the multipart boundary is set up right and the file content is encoded properly. The TgBot library’s HttpClientWrapper should handle the multipart encoding automatically when you set that third parameter to true, but different library versions can behave differently.
I encountered a similar issue while working with TgBot-cpp for file transfers. It seems you’re sending the file path with the ‘@’ symbol. This approach is specific to CURL, and the HttpClientWrapper needs the actual file content instead. Use std::ifstream to read the file into a std::string and then send that string, not just a reference to the path. Additionally, ensure your bot has the right permissions and that the file size complies with Telegram’s limits. Make sure the Content-Type is set to multipart/form-data, as some libraries may not do this by default. The 404 error suggests the request might not be formatted correctly as a POST, so double-check that it’s using the POST method.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.