Skip to content

Commit b230698

Browse files
committed
Support additional authorization schemes
The client always appends "Basic" to the authorization header, however there are other auth schemes that can be used: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication For example "Bearer" when using OAuth. This PR adds a `setAuthorizationType` method to the HTTPClient which allows this scheme to be configured by the caller. Authorization type is set to "Basic" by default so this will have no impact on existing usecases.
1 parent caa8d07 commit b230698

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,17 @@ void HTTPClient::setAuthorization(const char * auth)
463463
}
464464
}
465465

466+
/**
467+
* set the Authorization type for the http request
468+
* @param authType const char *
469+
*/
470+
void HTTPClient::setAuthorizationType(const char * authType)
471+
{
472+
if(authType) {
473+
_authorizationType = authType;
474+
}
475+
}
476+
466477
/**
467478
* set the timeout (ms) for establishing a connection to the server
468479
* @param connectTimeout int32_t
@@ -1177,8 +1188,9 @@ bool HTTPClient::sendHeader(const char * type)
11771188
}
11781189

11791190
if(_base64Authorization.length()) {
1180-
_base64Authorization.replace("\n", "");
1181-
header += F("Authorization: Basic ");
1191+
header += F("Authorization: ");
1192+
header += _authorizationType;
1193+
header += " ";
11821194
header += _base64Authorization;
11831195
header += "\r\n";
11841196
}

libraries/HTTPClient/src/HTTPClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class HTTPClient
171171
void setUserAgent(const String& userAgent);
172172
void setAuthorization(const char * user, const char * password);
173173
void setAuthorization(const char * auth);
174+
void setAuthorizationType(const char * authType);
174175
void setConnectTimeout(int32_t connectTimeout);
175176
void setTimeout(uint16_t timeout);
176177

@@ -251,6 +252,7 @@ class HTTPClient
251252
String _headers;
252253
String _userAgent = "ESP32HTTPClient";
253254
String _base64Authorization;
255+
String _authorizationType = "Basic";
254256

255257
/// Response handling
256258
RequestArgument* _currentHeaders = nullptr;

0 commit comments

Comments
 (0)