Skip to content

Commit

Permalink
feat(http): Allow to set Accept-Encoding header
Browse files Browse the repository at this point in the history
Similar to setUserAgent
  • Loading branch information
me-no-dev committed Jun 14, 2024
1 parent cbf1e94 commit 687df84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 11 additions & 3 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 += ": ";
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/HTTPClient/src/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 687df84

Please sign in to comment.