Parse HTML GET command

Hi there!

I am making an Ethernet billboard using an ethernet shield and a 16x2 display.
I've got the webpage working, but now i need to retrieve the data from the textboxes on the webpage.
I have 2 textboxes, one for the first row on the display and the other one for the second row on the display.

When I press submit on the webpage, the arduino receives this

GET /?fline=The+first+line&sline=The+second+line HTTP/1.1

Now I want to parse this into two strings, with the first string containing the first line and the other string containing the second line.
So the first string would contain "The first line" and the second string contains "The second line"

How can I parse this string into those 2 strings?

Thanks in advance! :slight_smile:

(edit. Forgot to mention that fline is the first line and sline is the second line)

That's not HTML, it's HTTP.
You should be using HTTP POST, not GET.

If you use plain text encoding (or multipart), it might be easier to parse, especially if your text contains special characters.

It'll be even easier if you use a single text field for both lines (just separate them with a new line, which is also more user friendly, IMHO).

If you use HTTP POST, not only do you prevent problems when your browser decides to cache or preload the web page, it'll also be easier to parse, since all data will be in the request body, that starts after the first empty line.

If you've got enough available memory on your Arduino, use an HTTP library rather than trying to parse the requests yourself.

Pieter

I don’t think i can use a HTTP library since the arduino acts like a webserver, and the HTTP library i found was a client. (GitHub - amcewen/HttpClient: Arduino HTTP library)

But I’ve changed the HTML code to use POST instead of GET, but now the data I receive on the serial monitor is POST / HTTP/1.1

What is going wrong here?

My code is

#include <SPI.h>
#include <Ethernet.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


IPAddress ip(192, 168, 0, 20);   // IP address, may need to change depending on network
EthernetServer server(80);       // create a server at port 80

String readString;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // Set the LCD I2C address

void setup()
{
  // disable Ethernet chip
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  pinMode(4, OUTPUT);

  Serial.begin(115200);       // for debugging

  lcd.begin(16, 2);
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  // Message on LCD
  lcd.setCursor(2, 0);
  lcd.print("Initializing");
  Serial.println("Initializing...");

  // initialize SD card

  if (Ethernet.begin(mac) == 0) {
    lcd.clear();
    lcd.setCursor(2, 0);
    lcd.print(F("DHCP Failed!"));
    lcd.setCursor(5, 1);
    lcd.print(F("Halted"));
    Serial.println("DHCP Failed!");
    Serial.println("Application halted");
    LoopForever();
  }
  server.begin();           // start to listen for clients

  // End of initialization message on LCD + print IP address
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("IP:"));
  lcd.setCursor(0, 1);
  lcd.print(Ethernet.localIP());
  Serial.print("Current IP: ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          //now output HTML data header
          if (readString.indexOf('?') >= 0) { //don't send new page
            client.println("HTTP/1.1 204 Sander");
            client.println();
            client.println();
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<title>Network billboard</title>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Network billboard</H1>");

            // Dit gedeelte HTML code is zo omgezet dat het wel meegecompileerd kan worden in C
            // Gebruikte website: http://tomeko.net/online_tools/cpp_text_escape.php?lang=en

            client.println("<form action=\"\" method=\"post\">"); //"<form action="" method="post">"
            client.println("First line: <input type=\"text\" name=\"fline\" maxlength=\"50\" autocomplete=\"off\" autocorrect=\"off\">
"); //"First line: <input type="text" name="fline" maxlength="50" autocomplete="off" autocorrect="off">
"

            client.println("Second line: <input type=\"text\" name=\"sline\" maxlength=\"50\" autocomplete=\"off\" autocorrect=\"off\">
"); //"Second line: <input type="text" name="sline" maxlength="50" autocomplete="off" autocorrect="off">
"
            client.println("<input type=\"submit\" value=\"Submit\">"); //<input type="submit" value="Submit">
            client.println("</form>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          Serial.println(readString);

          // Parse incoming data code goes here //

          /////////////////////////////////////////////         

          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}

void LoopForever()
{
  // Looping forever here
  LoopForever();
}
//if HTTP request has ended
        if (c == '\n') {

Because you don't read the entire request, you're only reading the first line.

I'm pretty sure there are HTTP server libraries as well.

Thanks!

I forgot to remove that bit of code from my previous attempt :sweat_smile:

Also I'll edit my code to make it use the Webduino library.

(edit. I got it to work! Thanks for all the help! :slight_smile: )