need help for establish the http stream link

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

need help for establish the http stream link

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

i try to establish the http stream link, but meet some error that i cann't locate。
i has already check my server code by postman,it run sucessufully
here is my 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

and my code
  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);

boarchuz
Posts: 605
Joined: Tue Aug 21, 2018 5:28 am

Re: need help for establish the http stream link

Postby boarchuz » Thu Feb 16, 2023 7:54 am

You are sending both the content length header and a chunked encoding header. This is not a valid HTTP request.

esp_http_client will take care of this for you based on the length you provide to esp_http_client_open (see https://github.com/espressif/esp-idf/bl ... 1344-L1348). Don't set either of these headers yourself.

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

Re: need help for establish the http stream link

Postby title0577 » Fri Feb 17, 2023 3:10 am

thank for your reply, i has fix this problem already。

Who is online

Users browsing this forum: No registered users and 95 guests