Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions examples/HelloGetArgument/HelloGetArgument.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

#include <ArduinoHttpServer.h>

#include <Arduino.h>


#ifdef ESP8266 // This example is compatible with both, ATMega and ESP8266
#include <ESP8266WiFi.h>
#else
#include <SPI.h> //! \todo Temporary see fix: https://github.com/platformio/platformio/issues/48
// on older Arduinos use WiFi.h, on Arduino Uno R4 WiFi use WiFiS3.h
#include <WiFiS3.h>
#endif

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";

WiFiServer wifiServer(80);

void setup()
{
Serial.begin(115200);
Serial.println("Starting Wifi Connection...");

WiFi.begin(const_cast<char*>(ssid), password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("Connected!");

wifiServer.begin();
}

void loop()
{
WiFiClient client( wifiServer.available() );
if (client.connected())
{
Serial.println("client connected!");
// Connected to client. Allocate and initialize StreamHttpRequest object.
ArduinoHttpServer::StreamHttpRequest<1024> httpRequest(client);

if (httpRequest.readRequest())
{
Serial.println("Done read request!");

// Retrieve HTTP resource / URL requested
Serial.println( httpRequest.getResource().toString() );

// Retrieve 1st part of HTTP resource.
// E.g.: "api" from "/api/sensors/on"
Serial.println(httpRequest.getResource()[0]);

ArduinoHttpServer::StreamHttpReply httpReply(client, "text/html");
String textField = httpRequest.getResource().getArgument("textfield");
Serial.print("Textfield is: ");
Serial.println(textField);

String reply = "<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>Minimal HTML Page</title>\n"
"</head>\n"
"<body>\n";
if(textField.length() > 0) {
reply += "<h3> Hello, ";
reply += textField;
reply += "!</h3>";
}
reply += " <form>\n"
" <label for=\"textfield\">Enter text:</label><br>\n"
" <input type=\"text\" id=\"textfield\" name=\"textfield\"><br><br>\n"
" <input type=\"submit\" value=\"Submit\">\n"
" </form>\n"
"</body>\n"
"</html>";
httpReply.send(reply);
}
else
{
// HTTP parsing failed. Client did not provide correct HTTP data or
// client requested an unsupported feature.
ArduinoHttpServer::StreamHttpErrorReply httpReply(client, httpRequest.getContentType());

const char *pErrorStr( httpRequest.getError().cStr() );
String errorStr(pErrorStr); //! \todo Make HttpReply FixString compatible.

httpReply.send( errorStr );
}
client.stop();
}

}
24 changes: 24 additions & 0 deletions src/internals/HttpResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ bool ArduinoHttpServer::HttpResource::isValid()
return m_resource.length() > 0;
}

// not a perfect solution, may return incorrect argument if one is a substring (at the start) of another
Copy link
Copy Markdown
Owner

@QuickSander QuickSander Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add D0xygen style commenting?

Thanks for the write up!

String ArduinoHttpServer::HttpResource::getArgument(const char *key) const {
int queryStart = m_resource.indexOf('?');
if (queryStart == -1) {
return "";
}

queryStart++;

String keyStr = String(key) + '=';
int keyStart = m_resource.indexOf(keyStr, queryStart);

if (keyStart == -1) {
return "";
}

int valueEnd = m_resource.indexOf('&', keyStart);
if (valueEnd == -1) {
valueEnd = m_resource.length();
}

String value = m_resource.substring(keyStart + keyStr.length(), valueEnd);
return value;
}
//! Retrieve resource part at the specified index.
//! \details E.g. HttpResource("/api/sensors/1/state")[1]
//! returns "sensors".
Expand Down
1 change: 1 addition & 0 deletions src/internals/HttpResource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class HttpResource

bool isValid();
String operator[](const unsigned int index) const;
String getArgument(const char *key) const;
const String& toString() const;

private:
Expand Down