Arduino Ethernet Shield Web Server Form Data

Hi Everyone,

Background: I am using my Arduino with the Arduino Ethernet Shield to create a web server. I am successfully serving up static pages to a browser. Next I've added code to create a FORM used to accept a GET method of posting http request data to the Arduino.

Problem: I don't know how to read the GET data from the post. I know the I can call the read() method from an instance of the Client object that returns a char. Is the posted data within those characters?

Code: Here's the code I'm using, modified from the Server example code that comes bundled with the Arduino development environment.

...
void loop()
{
Client client = server.available();
if (client)
{
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();

if (c == '\n' && current_line_is_blank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("

Arduino based web server.

");
client.println("<form name="input" action="myForm" method="get"> Data: <input type="text" name="data"> <input type="submit" value="Submit"> ");
client.println("
");
client.println(millis());
break;
}
if (c == '\n')
{
// we're starting a new line
current_line_is_blank = true;
} //end if
else if (c != '\r')
{
// we've gotten a character on the current line
current_line_is_blank = false;
} //end else if
} //end if (client.available())
} //end while (client.connected())
client.stop();

} //end if (client)
} //end void loop()

Thanks in advance for the help!

Mark~

Hi,

I still need to tidy this up a lot, but you might want to take a look at the most recent sort-of web framework for Arduino code I've uploaded.

It may not be useful for you in its current state but maybe it will be. :slight_smile: I haven't extracted it from what I'm using it for but wanted to at least suggest it if you were interested.

My suggestion would be to first modify the handleShow function to display your form. Then delete the calls to urlDispatcher.addHandler that don't apply to you and add something like this one:

urlDispatcher.addHandler("/myForm/?*", handleForm)

Then write a handleForm function that reads the remaining part of the URL.

Let me know if you try this and need some more pointers.

--Phil.

As you know, the request from the browser is a string. The values passed for GET or POST parameters are found in this string. The main difference between GET and POST is the location of these parameter values: GET passes params in the request string URL, POST passes them in the HTTP header.

This article may shed some light:

(I was trying to find something similar on IBM site :slight_smile: , took too long, so I chose sun's instead, I hope you don't mind :))

So, as soon as you have access to the HTTP request string, it is all yours to parse and analyze.

Hi Florinc,

Yes, I think (hopefully) I have a pretty good handle on the POST and GET form requests. Thanks for the link to Sun's website. :wink:

The last sentence you wrote is the key to my question - "as soon as you have access to the HTTP request string" - question is, how do I get access to the request string using the Ethernet Library? Once I have it, it'll just be an exercise in string manipulation (parsing) as you had stated.

Thanks for taking time to reply (and also dig through the IBM site). :slight_smile:

Mark~

Hi Everyone,

I've answered my own question. Each character read from the client.read() method returns a single character from the HTTP Request string. There's a whole bunch of data within the request, but the actual form post is near the beginning of the data. Here's the actual response from my test:

GET /myForm?data=This_is_the_form_data HTTP/1.1Host: 12.216.106.19User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 (CK-IBM) Firefox/3.0.7Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8Accept-Language: en-us,en;q=0.5Accept-Encoding: gzip,deflateAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive: 300Connection: keep-alive

As you can see, my actual form data is within the second token of the response:

/myForm?data=This_is_the_form_data

How did I figure this out? I used the Serial library to return each character read from the client to the Arduino IDE Serial Monitor. Worked great! Remember, in this situation, the "client" is the browser since the Arduino is the web server.

Thanks to everyone that took time to think about this or respond - good luck with your projects!

Mark~