From 687df8468675c6c3e0a11ab63852ba2afd574f68 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Fri, 14 Jun 2024 10:15:04 +0300 Subject: [PATCH] feat(http): Allow to set Accept-Encoding header Similar to setUserAgent --- libraries/HTTPClient/src/HTTPClient.cpp | 14 +++++++++++--- libraries/HTTPClient/src/HTTPClient.h | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 54eedf0ac2e..64dfe3a7db0 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -408,6 +408,14 @@ void HTTPClient::setUserAgent(const String &userAgent) { _userAgent = userAgent; } +/** + * set Accept Encoding Header + * @param acceptEncoding const char * + */ +void HTTPClient::setAcceptEncoding(const String &acceptEncoding) { + _acceptEncoding = acceptEncoding; +} + /** * set the Authorizatio for the http request * @param user const char * @@ -969,8 +977,8 @@ String HTTPClient::errorToString(int error) { */ void HTTPClient::addHeader(const String &name, const String &value, bool first, bool replace) { // not allow set of Header handled by code - if (!name.equalsIgnoreCase(F("Connection")) && !name.equalsIgnoreCase(F("User-Agent")) && !name.equalsIgnoreCase(F("Host")) - && !(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())) { + if (!name.equalsIgnoreCase(F("Connection")) && !name.equalsIgnoreCase(F("User-Agent")) && !name.equalsIgnoreCase(F("Accept-Encoding")) + && !name.equalsIgnoreCase(F("Host")) && !(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())) { String headerLine = name; headerLine += ": "; @@ -1130,7 +1138,7 @@ bool HTTPClient::sendHeader(const char *type) { header += "\r\n"; if (!_useHTTP10) { - header += F("Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n"); + header += String(F("Accept-Encoding: ")) + _acceptEncoding + F("\r\n"); } if (_base64Authorization.length()) { diff --git a/libraries/HTTPClient/src/HTTPClient.h b/libraries/HTTPClient/src/HTTPClient.h index 7841f355640..edc050ab0dd 100644 --- a/libraries/HTTPClient/src/HTTPClient.h +++ b/libraries/HTTPClient/src/HTTPClient.h @@ -194,6 +194,7 @@ class HTTPClient { void setReuse(bool reuse); /// keep-alive void setUserAgent(const String &userAgent); + void setAcceptEncoding(const String &acceptEncoding); void setAuthorization(const char *user, const char *password); void setAuthorization(const char *auth); void setAuthorizationType(const char *authType); @@ -285,6 +286,7 @@ class HTTPClient { String _userAgent = "ESP32HTTPClient"; String _base64Authorization; String _authorizationType = "Basic"; + String _acceptEncoding = "identity;q=1,chunked;q=0.1,*;q=0"; /// Response handling RequestArgument *_currentHeaders = nullptr;