esp_http_client: connection-keepalive when using stream reader API

doragasu
Posts: 16
Joined: Sat Apr 27, 2019 5:18 pm

esp_http_client: connection-keepalive when using stream reader API

Postby doragasu » Sat Apr 27, 2019 5:32 pm

I am using esp_http_client with the stream reader API to read server response data. It works perfect, but I want to reuse the same connection to perform several queries. I tried the following sequence and it did not work:
  1. // Init
  2. esp_http_client_init()
  3. esp_http_client_open()
  4.  
  5. // First request
  6. esp_http_client_fetch_headers()
  7. esp_http_client_read()
  8.  
  9. // Second request
  10. esp_http_client_fetch_headers()
  11. esp_http_client_read()
  12.  
  13. // Finish
  14. esp_http_client_close()
  15. esp_http_client_cleanup()
When I run this code, the first request is performed, but the second one is never done. After this issue, I browsed the esp_http_client code, and I saw that the esp_http_client_open() function performs some initialization required prior to the request, and that if the connection is already open, it does not try reopening it again, so I tried the following variation of the code:
  1. // Init
  2. esp_http_client_init()
  3.  
  4. // First request
  5. esp_http_client_open()
  6. esp_http_client_fetch_headers()
  7. esp_http_client_read()
  8.  
  9. // Second request
  10. esp_http_client_open()  // call open again to reinitialize the request
  11. esp_http_client_fetch_headers()
  12. esp_http_client_read()
  13.  
  14. // Finish
  15. esp_http_client_close()
  16. esp_http_client_cleanup()
I have tested this, and it is working, but it seems counterintuitive to do several esp_http_client_open() and only one esp_http_client_close(), so I am wondering if this might cause problems (maybe some resources not properly freed, or some other issue).

So is this OK? Is there any other way to reuse the connection for several requests when using the stream reader API?

Who is online

Users browsing this forum: MicroController and 136 guests