Go Down

Topic: Parse HTTP GET request recieved by ethernet shield (Read 58828 times) previous topic - next topic


Sep 01, 2010, 08:46 pm Last Edit: Sep 01, 2010, 08:47 pm by jraynes Reason: 1
I just bought an Arduino with an Ethernet shield and am trying to figure out how to parse a HTTP GET request like:

GET h ttp://arduino/?cmd1=functionA&cmd2=functionB HTTP/1.1

so that cmd1 and cmd2 are populated as variables that equal functionA and functionB during runtime respectively. Usually this would be done with regular expressions but I couldn't find a whole lot of documentation on how this works with the Arduino. If anyone could help me or point me to some example code on how to to this I would really appreciate it.

Thanks in advance,


How to parse the data depends to some extent on how you are storing the command received. Care to share that?

The standard C function, strtok, is available to use. There are sufficient delimiters in the command to allow extracting the tokens.

Are cmd1 and cmd2 constants in the command, or are supposed to represent variable values?


Below is some code I use to get GET request and parce out the desired info.

Code: [Select]

//zoomkat 5-24-10
// http://www.arduino.cc/en/Tutorial/TextString for WString.h
#include <WString.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString = String(100); //string for fetching data from address

String teststring = String(100);
String finalstring = String(100);
String flag = String(2);
int ind1 = 0;
int ind2 = 0;
int pos = 0;

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);

//enable serial data print
Serial.begin(9600); }

void loop(){
// Create a client connection
Client 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

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

//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

 if(readString.contains("-")) { //test for servo control sring
 readString.replace('-', '#');
 pos = readString.length(); //capture string length
 //find start of servo command string (#)
 ind1 = readString.indexOf('#');
 //capture front part of command string
 teststring = readString.substring(ind1, pos);
 //locate the end of the command string
 ind2 = teststring.indexOf(' ');
 //capturing the servo command string from readString
 finalstring = readString.substring(ind1, ind2+ind1);
 //print "finalstring" to com port;
 Serial.println(finalstring); //print string with CR
 //GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1
 if(readString.contains("Slidervalue")) {
 ind1 = readString.indexOf('u');
 ind2 = readString.indexOf('&');
 finalstring = readString.substring(ind1+1, ind2);
 finalstring.replace('e', '#');
 finalstring.replace('=', 'p');
 //now output HTML data header
 client.println("HTTP/1.1 204 Zoomkat");
 //stopping client

//clearing string for next read

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


That's exactly what I'm looking for, thanks!

Go Up