Oh god you’ve brought back memories of doing http download on a microcontroller.
First though:
Thank you for that code snippet, it makes anything else I see feel less awful by comparison
.
Agreed on the ‘option bag’ being much nicer, and in the http library I work with, that is in fact how you specify them:
esp_http_client_config_t config = {
.host = CONFIG_EXAMPLE_HTTP_ENDPOINT,
.path = "/get",
.query = "esp",
.event_handler = _http_event_handler,
.user_data = local_response_buffer,
.disable_auto_redirect = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
esp_http_client_cleanup(client);
Which works fine, but if you want to download multiple files from the same server then it’s really inefficient because:
- The TCP connection is killed and reopened for every request.
- You tear down and rebuild the entire HTTP engine each time, including all the memory and buffers and such.
This is why those ‘set’ functions are necessary: it lets you use a persistent connection for multiple requests:
esp_http_client_set_url(client, "/get2");
Or even:
esp_http_client_set_url(client, "/put");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
Either way, method-chaining is the absolute worst way to do this. How are the intermediate functions supposed to report an error? Exceptions? Yeah no, we disable those for a reason.
I could rant more, but this guy does it much better: