Arduino
Arduino copied to clipboard
Argument lookup method for ESP8266WebServer
Basic Infos
Hardware
Hardware: ESP8266 General Core Version: 2.0.0+
Description
Another (faster) way to get a ESP8266WebServer arg by name
ESP8266WebServer.cpp
// new method...
int ESP8266WebServer::hasArg(const char* name) {
for (int i = 0; i < _currentArgCount; ++i) {
if (_currentArgs[i].key == name)
return i;
}
return -1;
}
ESP8266WebServer.h
....
bool hasArg(const char* name); // check if argument exists
// new prototype..
int hasArg(const char* name); // check if argument exists and return index or -1 if arg not exists
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
....
sketch (sample)
.....
ESP8266WebServer esp8266WebServer(port);
int iArg;
String argName;
....
if ( (iArg = esp8266WebServer.has("argName")) >= 0)
argName = esp8266WebServer.arg(iArg);
....
C++ doesn't allow you to have two function overloads which only differ by their return type:
bool hasArg(const char* name); // check if argument exists
int hasArg(const char* name); // check if argument exists and return index or -1 if arg not exists
I think better naming for the function which you are proposing would be findArg.
Thanks Ivan ! Now I'll test this new method