esp_http_client get data with some error

zhoukuniyc
Posts: 8
Joined: Fri Sep 04, 2020 3:12 am

esp_http_client get data with some error

Postby zhoukuniyc » Sun Oct 11, 2020 11:03 am

I can get json data from url http://t.weather.itboy.net/api/weather/city/101030100 currectly , but when i use esp_http_client perform this query, I get HTTP_CLIENT: HTTP GET Status = 200, content_length = -1, and I can't get currect response , I use esp_http_client_read_response method to read the response ,

http://t.weather.itboy.net/api/weather/city/101030100
there is whole code
  1. static void http_rest_with_hostname_path(void)
  2. {
  3.     esp_http_client_config_t config = {
  4.         .host = "t.weather.itboy.net",
  5.         .path = "/api/weather/city/101030100",
  6.         .transport_type = HTTP_TRANSPORT_OVER_TCP,
  7.         .event_handler = _http_event_handler,
  8.     };
  9.     esp_http_client_handle_t client = esp_http_client_init(&config);
  10.  
  11.     // GET
  12.     esp_err_t err = esp_http_client_perform(client);
  13.     //  add logic code in here
  14.     int read_len = 0;
  15.     char buf[10024] = {0};
  16.     read_len = esp_http_client_read_response(client, buf, 10024);
  17.     ESP_LOGI(TAG, "HTTP GET response  success:len = %d data =  %100s", strlen(buf), buf);
  18.     if (err == ESP_OK)
  19.     {
  20.         ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
  21.                  esp_http_client_get_status_code(client),
  22.                  esp_http_client_get_content_length(client));
  23.     }
  24.     else
  25.     {
  26.         ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
  27.     }
  28.  
  29.     esp_http_client_cleanup(client);
  30. }


there is whole output:
I (2278) HTTP_CLIENT: HTTP GET response success:len = 529 data = "10","high":"高温 24℃","low":"低温 14℃","ymd":"2020-10-10","week":"星期六","sunrise":"06:16","sunset":"17:40","aqi":130,"fx":"南风","fl":"2级","type":"霾","notice":"雾霾来袭,戴好口罩再出门"}}}
0

�霾来袭,戴好口罩再出门"},{"date":"25","high":"高温 26℃","low":"低温 16℃","ymd":"2020-10-25","week":"星期日","sunrise":"06:31","sunset":"17:19","aqi":154,"fx":"南风","fl":"4级","type":"霾","notice":"雾霾来袭,戴好口罩再出门"}],"yesterday":{"date":m��?|��?X��?X��?
I (2318) HTTP_CLIENT: HTTP GET Status = 200, content_length = -1
I (2328) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
I (2328) HTTP_CLIENT: Finish http example

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_http_client get data with some error

Postby ESP_Sprite » Tue Oct 13, 2020 3:59 am

Not sure why the output is mangled, but the length of -1 indicates the response is chunked (meaning the server doesn't know the amount of bytes that are going to be sent at the time the header was sent), and that is expected for the kind of API interface you're calling.

Who is online

Users browsing this forum: No registered users and 348 guests