-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat(HTTPClient): add support for collecting all HTTP headers #11768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
69549c8
a2df766
3bd54ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <Arduino.h> | ||
|
||
#include <WiFi.h> | ||
#include <HTTPClient.h> | ||
|
||
// Enable or disable collecting all headers | ||
#define COLLECT_ALL_HEADERS true | ||
|
||
void setup() { | ||
|
||
Serial.begin(115200); | ||
|
||
Serial.println(); | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
for (uint8_t t = 4; t > 0; t--) { | ||
Serial.printf("[SETUP] WAIT %d...\n", t); | ||
Serial.flush(); | ||
delay(1000); | ||
} | ||
|
||
WiFi.begin("SSID", "PASSWORD"); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
Serial.println(); | ||
Serial.println("Connected to WiFi: " + WiFi.SSID()); | ||
} | ||
|
||
void loop() { | ||
|
||
HTTPClient http; | ||
|
||
Serial.print("[HTTP] Preparing HTTP request...\n"); | ||
// This page will return the headers we want to test + some others | ||
http.begin("https://httpbingo.org/response-headers?x-custom-header=value:42"); | ||
|
||
#if COLLECT_ALL_HEADERS | ||
// Collect all headers | ||
http.collectAllHeaders(); | ||
#else | ||
// Collect specific headers, only that one will be stored | ||
const char *headerKeys[] = {"x-custom-header"}; | ||
const size_t headerKeysCount = sizeof(headerKeys) / sizeof(headerKeys[0]); | ||
http.collectHeaders(headerKeys, headerKeysCount); | ||
#endif | ||
|
||
Serial.print("[HTTP] Sending HTTP GET request...\n"); | ||
// start connection and send HTTP header | ||
int httpCode = http.GET(); | ||
|
||
// httpCode will be negative on error | ||
if (httpCode > 0) { | ||
// HTTP header has been send and Server response header has been handled | ||
Serial.printf("[HTTP] GET response code: %d\n", httpCode); | ||
|
||
Serial.println("[HTTP] Headers collected:"); | ||
for (size_t i = 0; i < http.headers(); i++) { | ||
Serial.printf("[HTTP] - '%s': '%s'\n", http.headerName(i).c_str(), http.header(i).c_str()); | ||
} | ||
|
||
Serial.println("[HTTP] Has header 'x-custom-header'? " + String(http.hasHeader("x-custom-header")) + " (expected true)"); | ||
Serial.printf("[HTTP] x-custom-header: '%s' (expected 'value:42')\n", http.header("x-custom-header").c_str()); | ||
Serial.printf("[HTTP] non-existing-header: '%s' (expected empty string)\n", http.header("non-existing-header").c_str()); | ||
|
||
#if COLLECT_ALL_HEADERS | ||
// Server response with multiple headers, one of them is 'server' | ||
Serial.println("[HTTP] Has header 'server'? " + String(http.hasHeader("server")) + " (expected true)"); | ||
Serial.printf("[HTTP] server: '%s'\n", http.header("server").c_str()); | ||
#endif | ||
|
||
} else { | ||
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | ||
} | ||
|
||
http.end(); | ||
|
||
Serial.println("[HTTP] end connection\n\n"); | ||
delay(5000); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"requires_any": [ | ||
"CONFIG_SOC_WIFI_SUPPORTED=y", | ||
"CONFIG_ESP_WIFI_REMOTE_ENABLED=y" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
#include <NetworkClientSecure.h> | ||
#endif // HTTPCLIENT_NOSECURE | ||
|
||
/// Cookie jar support | ||
/// Cookie jar and header support | ||
#include <vector> | ||
|
||
#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000) | ||
|
@@ -238,6 +238,7 @@ class HTTPClient { | |
void addHeader(const String &name, const String &value, bool first = false, bool replace = true); | ||
|
||
/// Response handling | ||
void collectAllHeaders(bool collectAll = true); | ||
void collectHeaders(const char *headerKeys[], const size_t headerKeysCount); | ||
String header(const char *name); // get request header value by name | ||
String header(size_t i); // get request header value by number | ||
|
@@ -294,6 +295,7 @@ class HTTPClient { | |
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; | ||
bool _useHTTP10 = false; | ||
bool _secure = false; | ||
bool _collectAllHeaders = false; | ||
|
||
String _uri; | ||
String _protocol; | ||
|
@@ -304,8 +306,7 @@ class HTTPClient { | |
String _acceptEncoding = "identity;q=1,chunked;q=0.1,*;q=0"; | ||
|
||
/// Response handling | ||
RequestArgument *_currentHeaders = nullptr; | ||
size_t _headerKeysCount = 0; | ||
std::vector<RequestArgument> _currentHeaders; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why, vector probably needs more bytes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, the vector probably needs more bytes, but since I do not know the exact size and I am reading until the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully understand, over time every part of the core does need more resources. One day we will have a core which can not be used for anything more than just "blink". |
||
|
||
int _returnCode = 0; | ||
int _size = -1; | ||
|
Uh oh!
There was an error while loading. Please reload this page.