Keep persistent connection to server via http_client with different endpoints

DCSBL-
Posts: 6
Joined: Mon Dec 05, 2022 9:50 am

Keep persistent connection to server via http_client with different endpoints

Postby DCSBL- » 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:
  • `/` (method: GET) — no headers required.
  • `/state` (method: POST) — requires an Authorization header.
I want to use a persistent session with this server so that the SSL session does not need to be initiated with each request. To achieve this, I create a "client handle" that I keep alive (as documented in the ESP-IDF documentation). The problem I have is that the requests are mixed. Here is a simplified code example:

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
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_*`?

MicroController
Posts: 1554
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Keep persistent connection to server via http_client with different endpoints

Postby MicroController » Wed Sep 04, 2024 5:52 pm

Try calling esp_http_client_delete_header(client, "Authorization"); before performing the "GET /".

Or use two persistent connections, one for each endpoint.

DCSBL-
Posts: 6
Joined: Mon Dec 05, 2022 9:50 am

Re: Keep persistent connection to server via http_client with different endpoints

Postby DCSBL- » Thu Sep 05, 2024 11:18 am

Thank you @MicroController. I can work with your suggestion, but in my opinion it is a bit strange a persistent session is per URL, not per session. I think most http libraries in other programming languages do not work like that.

What is a good way to discuss a potential change or addition of this idea?

MicroController
Posts: 1554
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Keep persistent connection to server via http_client with different endpoints

Postby MicroController » Thu Sep 05, 2024 1:24 pm

I could argue that an esp_http_client instance is not a 'session'. (It also doesn't store any cookies...)
I'd like to hear your idea for improvement though. If you have a more-or-less specific request for functionality, https://github.com/espressif/esp-idf/issues would be the place to address it.

Given the current 'abstraction' level of the esp_http_client, I guess a reasonable function to ask for would be esp_http_client_reset(esp_http_client_handle_t hdl, esp_http_client_method_t m, const char* path) or the like.

DCSBL-
Posts: 6
Joined: Mon Dec 05, 2022 9:50 am

Re: Keep persistent connection to server via http_client with different endpoints

Postby DCSBL- » Thu Sep 05, 2024 1:47 pm

Good point about not calling it a session; it is indeed almost a session. Let's call it a 'persistent connection,' as stated in the documentation. ;)

esp_http_client_reset seems to be what we need. Any implementation using esp_http_client is dedicated to a specific service, so the user can decide if they need to use this.

I will gather some ideas and open an issue on GitHub. I have also opened this pull request, but we can postpone that one until everything is on the table.

Who is online

Users browsing this forum: No registered users and 61 guests