You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32-C3使用C语言读取Firebase API分块响应失败求助

ESP32-C3 Firebase登录API无法读取响应数据

我在为ESP32-C3开发C语言版Firebase登录API时,能够正常构造请求URL并发起POST请求,HTTP状态码返回200,但始终无法读取到响应数据,总读取长度为0。以下是相关代码、运行输出,以及功能正常的Python等效实现,恳请帮忙排查问题所在:

C语言实现代码

void firebase_login_user(Firebase* self, char* email, char* password) {
    char* url = malloc(strlen(self->api_key) + 78);
    if (url == NULL) {
        ESP_LOGE(TAG, "Error allocating memory for URL");
        return;
    }
    strcpy(url, "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=");
    char *full_url = strjoin(url, self->api_key);
    free(url); // Free the initial allocation

    if (!full_url) {
        ESP_LOGE(TAG, "Error joining URL");
        return;
    }

    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "email", email);
    cJSON_AddStringToObject(root, "password", password);
    cJSON_AddTrueToObject(root, "returnSecureToken");
    char *data = cJSON_Print(root);

    ESP_LOGI(TAG, "URL: %s", full_url);
    ESP_LOGI(TAG, "JSON: %s", data);

    esp_http_client_config_t config = {
        .url = full_url,
        .method = HTTP_METHOD_POST,
        .transport_type = HTTP_TRANSPORT_OVER_SSL,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
    if (client == NULL) {
        ESP_LOGE(TAG, "Failed to initialize HTTP client");
        free(full_url);
        cJSON_Delete(root);
        free(data);
        return;
    }
    esp_http_client_set_header(client, "Content-Type", "application/json; charset=UTF-8");
    esp_http_client_set_post_field(client, data, strlen(data));

    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "HTTP POST Status = %d, %s", err, esp_err_to_name(err));
        int http_status = esp_http_client_get_status_code(client);
        ESP_LOGI(TAG, "HTTP Status Code = %d", http_status);
        int content_length = esp_http_client_get_content_length(client);
        ESP_LOGI(TAG, "Content Length = %d", content_length);

        if (http_status == 200) {
            if (content_length == -1) {
                // Handle chunked transfer encoding
                ESP_LOGI(TAG, "Chunked response");
                char *buffer = malloc(512);
                if (buffer == NULL) {
                    ESP_LOGE(TAG, "Error allocating memory for buffer");
                } else {
                    int total_data_read = 0;
                    int data_read;
                    do {
                        data_read = esp_http_client_read(client, buffer, 512);
                        if (data_read > 0) {
                            buffer[data_read] = '\0'; // Null-terminate the response
                            ESP_LOGI(TAG, "Chunk: %s", buffer);
                            total_data_read += data_read;
                        } else if (data_read < 0) {
                            ESP_LOGE(TAG, "Error reading data");
                        }
                    } while (data_read > 0);
                    ESP_LOGI(TAG, "Total data read: %d", total_data_read);
                    free(buffer);
                }
            } else {
                // Handle non-chunked response
                char *buffer = malloc(content_length + 1);
                if (buffer == NULL) {
                    ESP_LOGE(TAG, "Error allocating memory for buffer");
                } else {
                    int total_data_read = esp_http_client_read(client, buffer, content_length);
                    if (total_data_read >= 0) {
                        buffer[total_data_read] = '\0'; // Null-terminate the response
                        ESP_LOGI(TAG, "Response: %s", buffer);
                    } else {
                        ESP_LOGE(TAG, "Error reading data");
                    }
                    free(buffer);
                }
            }
        } else {
            ESP_LOGE(TAG, "Unexpected HTTP status code: %d", http_status);
        }
    } else {
        ESP_LOGE(TAG, "Error performing HTTP request: %s", esp_err_to_name(err));
    }

    esp_http_client_cleanup(client);
    cJSON_Delete(root);
    free(data);
    free(full_url);
}

运行输出日志

I (12824) netlog: HTTP POST Status = 0, ESP_OK
I (12824) netlog: HTTP Status Code = 200
I (12824) netlog: Content Length = -1
I (12824) netlog: Chunked response
I (12834) netlog: Total data read: 0
I (12834) main_task: Returned from app_main()

功能正常的Python等效实现

def login_user(self, email, password):
    request_link = f"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={self.api_key}"
    headers = {"content-type": "application/json; charset=UTF-8"}
    data = json.dumps({"email": email, "password": password, "returnSecureToken": True})
    request_data = self.requests.post(request_link, headers=headers, data=data)
    self.error_http(request_data)
    self.currentUser = request_data.json()
    self.token = self.currentUser["idToken"]
    return request_data.json()

内容的提问来源于stack exchange,提问作者AzerSD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 23:14:52