From fad8a27cf74bbe70b041c8be89d069ce3c0fe2c4 Mon Sep 17 00:00:00 2001 From: Scott Shell Date: Thu, 19 Nov 2020 11:16:31 +0530 Subject: [PATCH] Make the UserAgent string in esp-http-client configurable Signed-off-by: Shubham Kulkarni Merges https://github.com/espressif/esp-idf/pull/6044 --- components/esp_http_client/esp_http_client.c | 6 ++++-- components/esp_http_client/include/esp_http_client.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 0f798bfa49..78f30ae857 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -569,9 +569,11 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co goto error; } + const char *user_agent = config->user_agent == NULL ? DEFAULT_HTTP_USER_AGENT : config->user_agent; + if (config->host != NULL && config->path != NULL) { _success = ( - (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) && + (esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) && (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK) ); @@ -582,7 +584,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co } else if (config->url != NULL) { _success = ( (esp_http_client_set_url(client, config->url) == ESP_OK) && - (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) && + (esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) && (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK) ); diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 2c5eb51746..d20a1aa8c7 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -110,6 +110,7 @@ typedef struct { const char *cert_pem; /*!< SSL server certification, PEM format as string, if the client requires to verify server */ const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */ const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */ + const char *user_agent; /*!< The User Agent string to send with HTTP requests */ esp_http_client_method_t method; /*!< HTTP Method */ int timeout_ms; /*!< Network timeout in milliseconds */ bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */