Skip to content

Commit 4aff6dd

Browse files
authored
Support additional authorization schemes (#5845)
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 bb50046 commit 4aff6dd

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

+14-1
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
@@ -1178,7 +1189,9 @@ bool HTTPClient::sendHeader(const char * type)
11781189

11791190
if(_base64Authorization.length()) {
11801191
_base64Authorization.replace("\n", "");
1181-
header += F("Authorization: Basic ");
1192+
header += F("Authorization: ");
1193+
header += _authorizationType;
1194+
header += " ";
11821195
header += _base64Authorization;
11831196
header += "\r\n";
11841197
}

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

+2
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)