Hi, thanks for your response.
It’s not because of the whitespace in the url, I just had to add that myself because the forum didn’t let me post more than 2 url:s in single message because I’m a new user on the forum.
Here’s how I make the call, I use libcurl and nlohmann json library.
using json = nlohmann::json; GeminiApiClient::GeminiApiClient(Logger& logger, MediaProcessor& media_processor, DatabaseManager& database_manager) : logger_(logger), media_processor_(media_processor), database_manager_(database_manager) { // Initialization if necessary } std::string GeminiApiClient::sendPhotoApiRequest(const std::string& photo_path) { try { std::string api_key = getApiToken("GOOGLE_GEMINI_API_KEY"); // Upload the file and get fileUri std::string file_uri = uploadFile(photo_path, "image/jpeg"); if (file_uri.empty()) { logger_.log(LogLevel::ERR, "Failed to upload image to Gemini."); return "[Error in Photo Analysis]"; } // Prepare the request payload json payload = { {"contents", json::array({ { {"role", "user"}, {"parts", json::array({ { {"fileData", { {"fileUri", file_uri}, {"mimeType", "image/jpeg"} }} }, { {"text", "Analyze the content of this image."} } })} } })}, {"generationConfig", { {"temperature", 1}, {"topK", 40}, {"topP", 0.95}, {"maxOutputTokens", 8192}, {"responseMimeType", "text/plain"} }} }; std::string request_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-8b:generateContent?key=" + api_key; HttpClient client(request_url); client.addHeader("Content-Type: application/json"); // Log the request payload logger_.log(LogLevel::DEBUG, "Sending request to Gemini API: " + payload.dump(2)); std::string response = client.fetchData(payload.dump()); // Log the response logger_.log(LogLevel::DEBUG, "Gemini API Response: " + response); // Parse the response json responseJson = json::parse(response); if (responseJson.contains("error")) { logger_.log(LogLevel::ERR, "API Error: " + responseJson["error"]["message"].get<std::string>()); return "[Error in Photo Analysis]"; } std::string analysis = responseJson["candidates"][0]["content"].get<std::string>(); return analysis; } catch (const std::exception& e) { logger_.log(LogLevel::ERR, "Error in sendPhotoApiRequest: " + std::string(e.what())); return "[Error in Photo Analysis]"; } }
#include <curl/curl.h> //in http_client.h HttpClient::HttpClient(const std::string& url) : url(url) { curl_global_init(CURL_GLOBAL_DEFAULT); } void HttpClient::addHeader(const std::string& header) { headers.push_back(header); } std::string HttpClient::fetchData(const std::string& jsonPayload) { std::string responseData; CURL* curl = curl_easy_init(); if (curl) { setCommonOptions(curl, responseData); // Set POST-specific options curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonPayload.c_str()); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } return responseData; }
I have to look into this tomorrow, maybe there is something wrong how the call itself is made.