请教:尝试建立http stream连接的问题

title0577
Posts: 3
Joined: Wed Feb 15, 2023 6:26 am

请教:尝试建立http stream连接的问题

Postby title0577 » Wed Feb 15, 2023 6:32 am

我在尝试建立http_stream链接,但是遇到了一个报错,我没办法定位到错误的地方
log内容为
I (7427) example: HTTP_EVENT_ON_CONNECTED
I (7437) example: HTTP_EVENT_HEADER_SENT
E (7437) example: Failed to upload data: ERROR
I (7437) example: HTTP_EVENT_DISCONNECTED

我的代码是
  1. esp_http_client_config_t config = {
  2.     .url = "http://192.168.206.200:3000/test",
  3.     .event_handler = http_event_handler,
  4.     .method = HTTP_METHOD_POST,
  5.     .timeout_ms = 5000,
  6.     .buffer_size = 1024,
  7.     .user_data = NULL,
  8.     .is_async = false,
  9.     .use_global_ca_store = true,
  10.     .disable_auto_redirect = false,
  11.     .max_redirection_count = 10,
  12.     .auth_type = HTTP_AUTH_TYPE_NONE,
  13.     // .auth_cred = {
  14.     //     .username = NULL,
  15.     //     .password = NULL,
  16.     //     .digest = NULL,
  17.     // },
  18.     .query = NULL,
  19.     // .header = {
  20.     //     .key = "Content-Type",
  21.     //     .value = "application/octet-stream",
  22.     // },
  23.     // .query_len = 0,
  24.     // .header_len = 0,
  25. };
  26.  
  27.  
  28.     esp_http_client_handle_t client = esp_http_client_init(&config);
  29.    
  30.     const char *data = "hello world!";
  31.     int data_len = strlen(data);
  32.     ESP_LOGI(TAG,"%d",data_len);
  33.     char data_len_str[10];
  34.     itoa(data_len, data_len_str, 10);
  35.     esp_http_client_set_header(client, "Transfer-Encoding", "chunked");
  36.     esp_http_client_set_header(client, "Content-Length","12");
  37.     esp_http_client_set_header(client, "Content-Type", "application/octet-stream");
  38.     esp_err_t err = esp_http_client_open(client, data_len);
  39.     if (err != ESP_OK) {
  40.         ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  41.         goto cleanup;
  42.     }
  43.     err = esp_http_client_write(client, data, data_len);
  44.     if (err != ESP_OK) {
  45.         ESP_LOGE(TAG, "Failed to upload data: %s", esp_err_to_name(err));
  46.         goto cleanup;
  47.     }
  48.     err = esp_http_client_fetch_headers(client);
  49.     if (err != ESP_OK) {
  50.         ESP_LOGE(TAG, "Failed to fetch HTTP response headers: %s", esp_err_to_name(err));
  51.         goto cleanup;
  52.     }
  53.     int status_code = esp_http_client_get_status_code(client);
  54.     ESP_LOGI(TAG, "HTTP response status code: %d", status_code);
  55.  
  56. cleanup:
  57.     esp_http_client_cleanup(client);
服务器端我已经使用postman测试过了,可以正常使用

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: 请教:尝试建立http stream连接的问题

Postby ESP_YJM » Thu Feb 16, 2023 11:36 am

esp_http_client_write 函数返回的是 -1(错误),大于等于 0 表示写入的数据,你用返回值不等于 ESP_OK 来判断是不对的。

Who is online

Users browsing this forum: No registered users and 83 guests