Skip to content

Commit a9ce1b4

Browse files
committed
add Authorization support for HTTP client
1 parent 62f38bf commit a9ce1b4

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

cores/esp8266/core_esp8266_features.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727

2828
#define CORE_HAS_LIBB64
29+
#define CORE_HAS_BASE64_CLASS
2930

3031

3132
#endif

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#include <ESP8266WiFi.h>
2727
#include <WiFiClientSecure.h>
2828
#include <StreamString.h>
29+
#include <base64.h>
2930

3031
#include "ESP8266HTTPClient.h"
3132

33+
3234
/**
3335
* constractor
3436
*/
@@ -219,6 +221,20 @@ void HTTPClient::setUserAgent(const char * userAgent) {
219221
_userAgent = userAgent;
220222
}
221223

224+
/**
225+
* set the Authorizatio for the http request
226+
* @param user const char *
227+
* @param password const char *
228+
*/
229+
void HTTPClient::setAuthorization(const char * user, const char * password) {
230+
if(user && password) {
231+
String auth = user;
232+
auth += ":";
233+
auth += password;
234+
_base64Authorization = base64::encode(auth);
235+
}
236+
}
237+
222238
/**
223239
* send a GET request
224240
* @return http code
@@ -490,7 +506,7 @@ String HTTPClient::errorToString(int error) {
490506
void HTTPClient::addHeader(const String& name, const String& value, bool first) {
491507

492508
// not allow set of Header handled by code
493-
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) {
509+
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host") && !(_base64Authorization.length() && name.equalsIgnoreCase("Authorization"))) {
494510
String headerLine = name;
495511
headerLine += ": ";
496512
headerLine += value;
@@ -622,7 +638,13 @@ bool HTTPClient::sendHeader(const char * type) {
622638
} else {
623639
header += "close";
624640
}
625-
header += "\r\n" + _Headers + "\r\n";
641+
header += "\r\n";
642+
643+
if(_base64Authorization.length()) {
644+
header += "Authorization: Basic " + _base64Authorization + "\r\n";
645+
}
646+
647+
header += _Headers + "\r\n";
626648

627649
return (_tcp->write(header.c_str(), header.length()) == header.length());
628650
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef ESP8266HTTPClient_H_
2626
#define ESP8266HTTPClient_H_
2727

28-
//#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
28+
#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
2929

3030
#ifndef DEBUG_HTTPCLIENT
3131
#define DEBUG_HTTPCLIENT(...)
@@ -121,6 +121,7 @@ class HTTPClient {
121121

122122
void setReuse(bool reuse); /// keep-alive
123123
void setUserAgent(const char * userAgent);
124+
void setAuthorization(const char * user, const char * password);
124125

125126
/// request handling
126127
int GET();
@@ -172,6 +173,7 @@ class HTTPClient {
172173

173174
String _Headers;
174175
String _userAgent;
176+
String _base64Authorization;
175177

176178
/// Response handling
177179
RequestArgument* _currentHeaders;

0 commit comments

Comments
 (0)