Skip to content

Commit 1efab83

Browse files
authored
feat(http): Allow to set Accept-Encoding header (#9863)
Similar to setUserAgent
1 parent 2c7f722 commit 1efab83

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Diff for: libraries/HTTPClient/src/HTTPClient.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,14 @@ void HTTPClient::setUserAgent(const String &userAgent) {
408408
_userAgent = userAgent;
409409
}
410410

411+
/**
412+
* set Accept Encoding Header
413+
* @param acceptEncoding const char *
414+
*/
415+
void HTTPClient::setAcceptEncoding(const String &acceptEncoding) {
416+
_acceptEncoding = acceptEncoding;
417+
}
418+
411419
/**
412420
* set the Authorizatio for the http request
413421
* @param user const char *
@@ -969,8 +977,8 @@ String HTTPClient::errorToString(int error) {
969977
*/
970978
void HTTPClient::addHeader(const String &name, const String &value, bool first, bool replace) {
971979
// not allow set of Header handled by code
972-
if (!name.equalsIgnoreCase(F("Connection")) && !name.equalsIgnoreCase(F("User-Agent")) && !name.equalsIgnoreCase(F("Host"))
973-
&& !(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())) {
980+
if (!name.equalsIgnoreCase(F("Connection")) && !name.equalsIgnoreCase(F("User-Agent")) && !name.equalsIgnoreCase(F("Accept-Encoding"))
981+
&& !name.equalsIgnoreCase(F("Host")) && !(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())) {
974982

975983
String headerLine = name;
976984
headerLine += ": ";
@@ -1130,7 +1138,7 @@ bool HTTPClient::sendHeader(const char *type) {
11301138
header += "\r\n";
11311139

11321140
if (!_useHTTP10) {
1133-
header += F("Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0\r\n");
1141+
header += String(F("Accept-Encoding: ")) + _acceptEncoding + F("\r\n");
11341142
}
11351143

11361144
if (_base64Authorization.length()) {

Diff for: libraries/HTTPClient/src/HTTPClient.h

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class HTTPClient {
194194

195195
void setReuse(bool reuse); /// keep-alive
196196
void setUserAgent(const String &userAgent);
197+
void setAcceptEncoding(const String &acceptEncoding);
197198
void setAuthorization(const char *user, const char *password);
198199
void setAuthorization(const char *auth);
199200
void setAuthorizationType(const char *authType);
@@ -285,6 +286,7 @@ class HTTPClient {
285286
String _userAgent = "ESP32HTTPClient";
286287
String _base64Authorization;
287288
String _authorizationType = "Basic";
289+
String _acceptEncoding = "identity;q=1,chunked;q=0.1,*;q=0";
288290

289291
/// Response handling
290292
RequestArgument *_currentHeaders = nullptr;

0 commit comments

Comments
 (0)