esp_http_client: connection-keepalive when using stream reader API
Posted: 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:
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:
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?
- // Init
- esp_http_client_init()
- esp_http_client_open()
- // First request
- esp_http_client_fetch_headers()
- esp_http_client_read()
- // Second request
- esp_http_client_fetch_headers()
- esp_http_client_read()
- // Finish
- esp_http_client_close()
- esp_http_client_cleanup()
- // Init
- esp_http_client_init()
- // First request
- esp_http_client_open()
- esp_http_client_fetch_headers()
- esp_http_client_read()
- // Second request
- esp_http_client_open() // call open again to reinitialize the request
- esp_http_client_fetch_headers()
- esp_http_client_read()
- // Finish
- esp_http_client_close()
- esp_http_client_cleanup()
So is this OK? Is there any other way to reuse the connection for several requests when using the stream reader API?