diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index ef9721125a..60893893ec 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -756,6 +756,18 @@ esp_err_t esp_websocket_client_set_headers(esp_websocket_client_handle_t client, return ret; } +esp_err_t esp_websocket_client_set_header(esp_websocket_client_handle_t client, const char *key, const char *value) +{ + if (client == NULL || client->state != WEBSOCKET_STATE_CONNECTED || key == NULL) { + return ESP_ERR_INVALID_ARG; + } + + xSemaphoreTakeRecursive(client->lock, portMAX_DELAY); + esp_err_t ret = esp_transport_ws_set_header(client->transport, key, value); + xSemaphoreGiveRecursive(client->lock); + return ret; +} + static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client) { int rlen; diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index 318ae8bc4e..579d7491d5 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -165,6 +165,21 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con */ esp_err_t esp_websocket_client_set_headers(esp_websocket_client_handle_t client, const char *headers); +/** + * @brief Set websocket headers for the client + * + * Notes: + * - should not be used together with `esp_websocket_client_set_headers` + * + * @param[in] client The client + * @param[in] key The header key + * @param[in] value The header value + + * + * @return esp_err_t + */ +esp_err_t esp_websocket_client_set_header(esp_websocket_client_handle_t client, const char *key, const char *value); + /** * @brief Open the WebSocket connection *