I found a post server example in the ESP8266-Webserver-example list. Then I modified it to reach my need. I wrote my own HTML code and hosted that site in my ESP8266. The code is:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "pass"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = LED_BUILTIN;
const String postForms = "";
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/html", postForms);
digitalWrite(led, 0);
}
void handlePlain() {
if (server.method() != HTTP_POST) {
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
} else {
digitalWrite(led, 1);
server.send(200, "text/plain", "POST body was:\n" + server.arg("plain"));
digitalWrite(led, 0);
}
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/postplain/", handlePlain);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}
And when I enter this HTML code inside of the postForms string, I get the correct output. Here:
<html>
<head>
<title>ESP8266 Web Server POST handling</title>
<style>
body { background-color: cyan; font-family: Arial; Color: Black}
</style>
</head>
<body>
<h1>Let us know what you think!</h1><br>
<form method="post" enctype="text/plain" action="/postplain/" >
<input type="text" name="Comment:" value=""><br>
<input type="submit" value="Submit">
</form>
The output after submitting "Hello" using the above HTML code:
POST body was:
Comment:=Hello
But when I use the <textarea>
element instead of the <input>
element in the HTML code:
<html>
<head>
<title>ESP8266 Web Server POST handling</title>
<style>
body { background-color: cyan; font-family: Arial; Color: Black}
</style>
</head>
<body>
<h1>Let us know what you think!</h1><br>
<form method="post" enctype="text/plain" action="/postplain/" >
<textarea rows = "10" cols = "60">Hello</textarea> <br><br><br>
<input type="submit">
</form>
Then the output is:
POST body was:
Using the <textarea>
element, the server.arg("plain") function is not printing the input value.
Am I doing something wrong? Any help appreciated.
Thanks!
I found the answer to my question myself after trying for some time. I need to add the name
attribute inside the <textarea>
element like this: <textarea rows = "10" cols = "60" name = "somewhat_name">Hello</textarea>
The implementation of the server.arg() function seems to be a bit weird, it doesn't return the input value unless the <input>
or <textarea>
element has the name
attribute with something in it.