Keep persistent connection to server via http_client with different endpoints
Posted: Wed Sep 04, 2024 1:25 pm
Hello!
I am using the `http_client` component to fetch and send data to an HTTPS server. This server has two endpoints:
Is there any way to keep the session alive without having to reset the client handle, and preferably without having to reset each item I've set via `esp_http_client_set_*`?
I am using the `http_client` component to fetch and send data to an HTTPS server. This server has two endpoints:
- `/` (method: GET) — no headers required.
- `/state` (method: POST) — requires an Authorization header.
Code: Select all
esp_http_client_config_t config = {
.host = <host>,
.port = 443,
.path = "/",
.event_handler = _http_event_handler,
.transport_type = HTTP_TRANSPORT_OVER_SSL,
.keep_alive_enable = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// 1. GET /
esp_http_client_set_url(client, "/");
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_http_client_perform(client); // -> OK, data received in event handler
// 2. POST /state
esp_http_client_set_url(client, "/state");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, "{\"state\": \"on\"}", 15);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Authorization", <token>);
esp_http_client_perform(client); // -> OK, data received in event handler
// 3. GET /
esp_http_client_set_url(client, "/");
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_http_client_perform(client); // -> FAIL: Sends the Authorization header, which is rejected by the server