How to post a photo to Telegram api?

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

How to post a photo to Telegram api?

Postby hououin_kyouma » Sun Nov 05, 2023 10:14 am

Hello, I am using ESP32-CAM board and trying to post a photo to Telegram api. I've tried to send a text message and take a picture. They are working. But I can not send a photo to api. It gives a response like below:
  1. I (6517) Sending sendMessage: Starting Taking Picture!
  2. I (6517) Sending sendMessage: pic->len=7119
  3. W (6527) Sending sendMessage: Iniciare
  4. W (6527) Sending sendMessage: Enviare POST
  5. I (8097) Sending sendMessage: HTTP POST Status = 400, content_length = 91
  6. W (8107) Sending sendMessage: Desde Perform el output es: {"ok":false,"error_code":400,"description":"Bad Request: there is no photo in the request"}
And here is my post a photo code below:
  1. static esp_err_t https_telegram_sendPicture_perform_post()
  2. {
  3. ESP_LOGI(TAG3, "Starting Taking Picture!");
  4.  
  5.     camera_fb_t *pic = esp_camera_fb_get();
  6.  
  7.     if (!pic)
  8.     {
  9.         ESP_LOGE(TAG3, "Camera capture failed");
  10.  
  11.         return ESP_FAIL;
  12.     }
  13.     else
  14.     {
  15.         ESP_LOGI(TAG3, "pic->len=%d", pic->len);
  16.     }
  17.  
  18. char url[512] = "";
  19.  
  20.     char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};   // Buffer to store response of http request
  21.     // char output_buffer[1024*10] = {0};   // Buffer to store response of http request
  22.  
  23.     esp_http_client_config_t config =
  24.     {
  25.         .url = "https://api.telegram.org",
  26.         .transport_type = HTTP_TRANSPORT_OVER_SSL,
  27.         .event_handler = _http_event_handler,
  28.         .cert_pem = telegram_certificate_pem_start,
  29.         .user_data = output_buffer,
  30.     };
  31.  
  32.     //POST
  33.     ESP_LOGW(TAG3, "Iniciare");
  34.  
  35.     esp_http_client_handle_t client = esp_http_client_init(&config)
  36.  
  37.     //Copy the url+TOKEN
  38.     strcat(url, url_string);
  39.     //Passing the method
  40.     strcat(url, "/sendPhoto");
  41.  
  42.     esp_http_client_set_url(client, url);
  43.  
  44.     ESP_LOGW(TAG3, "Enviare POST");
  45.  
  46.     char post_data[1024*10] = "";
  47.     sprintf(post_data,"{\"chat_id\":%s,\"photo\":%s}", chat_ID2, (const char *) pic->buf);
  48.  
  49.     esp_http_client_set_method(client, HTTP_METHOD_POST);
  50.     esp_http_client_set_header(client, "Content-Type", "image/jpeg");
  51.     esp_http_client_set_post_field(client, (const char*) post_data, strlen(post_data));
  52.  
  53.     esp_err_t err = esp_http_client_perform(client);
  54.     if (err == ESP_OK)
  55.     {
  56.         ESP_LOGI(TAG3, "HTTP POST Status = %ld, content_length = %ld",
  57.                 (long)esp_http_client_get_status_code(client),
  58.                 (long)esp_http_client_get_content_length(client));
  59.         ESP_LOGW(TAG3, "Desde Perform el output es: %s",output_buffer);
  60.  
  61.     }
  62.     else
  63.     {
  64.         ESP_LOGE(TAG3, "HTTP POST request failed: %s", esp_err_to_name(err));
  65.     }
  66.  
  67.     ESP_LOGW(TAG, "Limpiare");
  68.     esp_http_client_close(client);
  69.     esp_http_client_cleanup(client);
  70.     ESP_LOGI(TAG3, "esp_get_free_heap_size: %lu", esp_get_free_heap_size ());
  71.  
  72.     esp_camera_fb_return(pic);
  73.  
  74.     return ESP_OK;
  75. }
I've stucked to the same error by days. Thanks for your attention.

ESP_adokitkat
Posts: 50
Joined: Thu Jun 22, 2023 12:50 pm

Re: How to post a photo to Telegram api?

Postby ESP_adokitkat » Mon Nov 06, 2023 3:00 am

Hello.

Are you sure you can just insert binary data as string into json string? According to Telegram bot API you should use multipart/form-data POST method.

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

Re: How to post a photo to Telegram api?

Postby hououin_kyouma » Mon Nov 06, 2023 7:37 pm

Hello. I've changed the 'image/jpeg' to 'multipart/form-data'.

Code: Select all

esp_http_client_set_header(client, "Content-Type", "multipart/form-data");
Even if I try to post with 'multipart/form-data' I got a response like below:

Code: Select all

I (8977) Sending sendMessage: Starting Taking Picture!
I (8987) Sending sendMessage: pic->len=8272
W (8997) Sending sendMessage: Iniciare
I (8997) Sending sendMessage: url string es: https://api.telegram.org/bot[token]/sendPhoto
W (9007) Sending sendMessage: Enviare POST
W (9017) HTTP_CLIENT Handler: El json es es: {"chat_id":1111111,"photo":����}
I (10877) Sending sendMessage: HTTP POST Status = 400, content_length = 0
W (10877) Sending sendMessage: Desde Perform el output es:
W (10877) HTTP_CLIENT Handler: Limpiare
I (10887) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (10897) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (10897) Sending sendMessage: esp_get_free_heap_size: 2125792
I (10907) HTTP_CLIENT Handler: Finish http example
What is {"photo":����} exactly mean? I can not insert binary data to json? And in this line:

Code: Select all

W (10877) Sending sendMessage: Desde Perform el output es:
It can not get any http response.

Thanks for your attention.

ESP_adokitkat
Posts: 50
Joined: Thu Jun 22, 2023 12:50 pm

Re: How to post a photo to Telegram api?

Postby ESP_adokitkat » Wed Nov 08, 2023 1:58 pm

hououin_kyouma wrote:
Mon Nov 06, 2023 7:37 pm
Hello. I've changed the 'image/jpeg' to 'multipart/form-data'.

Code: Select all

esp_http_client_set_header(client, "Content-Type", "multipart/form-data");
Even if I try to post with 'multipart/form-data' I got a response like below:

Code: Select all

I (8977) Sending sendMessage: Starting Taking Picture!
I (8987) Sending sendMessage: pic->len=8272
W (8997) Sending sendMessage: Iniciare
I (8997) Sending sendMessage: url string es: https://api.telegram.org/bot[token]/sendPhoto
W (9007) Sending sendMessage: Enviare POST
W (9017) HTTP_CLIENT Handler: El json es es: {"chat_id":1111111,"photo":����}
I (10877) Sending sendMessage: HTTP POST Status = 400, content_length = 0
W (10877) Sending sendMessage: Desde Perform el output es:
W (10877) HTTP_CLIENT Handler: Limpiare
I (10887) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (10897) HTTP_CLIENT Handler: HTTP_EVENT_DISCONNECTED
I (10897) Sending sendMessage: esp_get_free_heap_size: 2125792
I (10907) HTTP_CLIENT Handler: Finish http example
What is {"photo":����} exactly mean? I can not insert binary data to json? And in this line:

Code: Select all

W (10877) Sending sendMessage: Desde Perform el output es:
It can not get any http response.

Thanks for your attention.

No, you cannot use JSON like that with "multipart/form-data" as well as embedding binary data into any other text data, including JSON.

I found some examples for you:
https://developer.mozilla.org/en-US/doc ... %20type%3A
https://stackoverflow.com/a/46150309
https://github.com/nopnop2002/esp-idf-m ... #L110-L147
https://stackoverflow.com/a/73937408

You need to construct HTTP POST request yourself and attach the data there. I am no expert on the matter though.

hououin_kyouma
Posts: 7
Joined: Fri Nov 03, 2023 9:56 pm

Re: How to post a photo to Telegram api?

Postby hououin_kyouma » Mon Nov 13, 2023 6:12 pm

Thanks a lot, after days finally I can post multipart/data-form :D

Who is online

Users browsing this forum: No registered users and 62 guests