gemini post returns response 200, but the result is empty
Posted: Tue Feb 04, 2025 3:19 pm
I am trying to get response from gemini. post returns response 200, but the result is empty!. ca_certificate problem ?
// Send HTTP POST request to the Gemini API
void send_gemini_request(void) {
char question[200] = "What is Todays Significance?";
ESP_LOGI(TAG, "Asking Your Question: %s", question);
esp_http_client_config_t config = {
.url = "https://generativelanguage.googleapis.c ... ntent?key=" GEMINI_API_KEY,
.method = HTTP_METHOD_POST,
.cert_pem = postman_root_cert_pem_start, // Add server certificate if required
.buffer_size = 2048, // Increase buffer size if needed
.disable_auto_redirect = false,
.keep_alive_enable = true,
.user_agent = "ESP32",
};
ESP_LOGI(TAG, "URL: %s", config.url);
esp_http_client_handle_t client = esp_http_client_init(&config);
// Prepare the payload
// char payload[512];
const char *payload = "{"
"\"contents\": [{"
"\"parts\": [{"
"\"text\": \"What is Todays Significance?\""
"}]"
"}],"
"\"generationConfig\": {"
"\"maxOutputTokens\": 800"
"}"
"}";
//snprintf(payload, sizeof(payload), "{\"contents\": [{\"parts\":[{\"text\":%s}]}],\"generationConfig\": {\"maxOutputTokens\": %s}}", question, GEMINI_MAX_TOKENS);
ESP_LOGI(TAG, "Payload: %s", payload);
// Set headers
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Authorization", GEMINI_API_KEY);
esp_http_client_set_method(client, HTTP_METHOD_POST);
// Send the request
esp_http_client_set_post_field(client, payload, strlen(payload));
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
int status_code = esp_http_client_get_status_code(client);
ESP_LOGI(TAG, "Status Code: %d", status_code);
if (status_code == 200) {
long contentlength = esp_http_client_get_content_length(client);
ESP_LOGI(TAG, "Received Content Length: %ld", contentlength);
if(contentlength>0){
char* response = malloc(contentlength + 1);
esp_http_client_read(client, response, esp_http_client_get_content_length(client));
response[esp_http_client_get_content_length(client)] = '\0';
}
ESP_LOGI(TAG, "response: %s", payload);
} else {
ESP_LOGE(TAG, "HTTP request failed with status code: %d", status_code);
}
} else {
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
I (12184) mbedtls: ssl_msg.c:4206 <= read record
I (12194) mbedtls: ssl_msg.c:5867 <= read
I (12204) GEMINI_EXAMPLE: Status Code: 200
I (12204) GEMINI_EXAMPLE: Received Content Length: -1
I (12214) GEMINI_EXAMPLE: response: {"contents": [{"parts": [{"text": "What is Todays Significance?"}]}],"generationConfig": {"maxOutputTokens": 800}}
// Send HTTP POST request to the Gemini API
void send_gemini_request(void) {
char question[200] = "What is Todays Significance?";
ESP_LOGI(TAG, "Asking Your Question: %s", question);
esp_http_client_config_t config = {
.url = "https://generativelanguage.googleapis.c ... ntent?key=" GEMINI_API_KEY,
.method = HTTP_METHOD_POST,
.cert_pem = postman_root_cert_pem_start, // Add server certificate if required
.buffer_size = 2048, // Increase buffer size if needed
.disable_auto_redirect = false,
.keep_alive_enable = true,
.user_agent = "ESP32",
};
ESP_LOGI(TAG, "URL: %s", config.url);
esp_http_client_handle_t client = esp_http_client_init(&config);
// Prepare the payload
// char payload[512];
const char *payload = "{"
"\"contents\": [{"
"\"parts\": [{"
"\"text\": \"What is Todays Significance?\""
"}]"
"}],"
"\"generationConfig\": {"
"\"maxOutputTokens\": 800"
"}"
"}";
//snprintf(payload, sizeof(payload), "{\"contents\": [{\"parts\":[{\"text\":%s}]}],\"generationConfig\": {\"maxOutputTokens\": %s}}", question, GEMINI_MAX_TOKENS);
ESP_LOGI(TAG, "Payload: %s", payload);
// Set headers
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Authorization", GEMINI_API_KEY);
esp_http_client_set_method(client, HTTP_METHOD_POST);
// Send the request
esp_http_client_set_post_field(client, payload, strlen(payload));
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
int status_code = esp_http_client_get_status_code(client);
ESP_LOGI(TAG, "Status Code: %d", status_code);
if (status_code == 200) {
long contentlength = esp_http_client_get_content_length(client);
ESP_LOGI(TAG, "Received Content Length: %ld", contentlength);
if(contentlength>0){
char* response = malloc(contentlength + 1);
esp_http_client_read(client, response, esp_http_client_get_content_length(client));
response[esp_http_client_get_content_length(client)] = '\0';
}
ESP_LOGI(TAG, "response: %s", payload);
} else {
ESP_LOGE(TAG, "HTTP request failed with status code: %d", status_code);
}
} else {
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
I (12184) mbedtls: ssl_msg.c:4206 <= read record
I (12194) mbedtls: ssl_msg.c:5867 <= read
I (12204) GEMINI_EXAMPLE: Status Code: 200
I (12204) GEMINI_EXAMPLE: Received Content Length: -1
I (12214) GEMINI_EXAMPLE: response: {"contents": [{"parts": [{"text": "What is Todays Significance?"}]}],"generationConfig": {"maxOutputTokens": 800}}