Need help in parsing a char array (HTTP GET request) please


Im having problems parsing data in C.
I have spent about 20 hours working on this problem and after some initial progress im stuck parsing the rest of the data to get a format i can work with.

Please keep in mind that i am fairly new to C in your response :slight_smile:

Some background:
I have followed examples and tutorials online in order to get a working webserver running on my Arduino Mega.
I have made a webpage which utilizes JS/Ajax in order to manipulate various outputs on the arduino, this is sent with a HTTP GET request. This request is stored in the “incoming” array below, i did this in order to work on the parsing without running the entire code.

My goal is to parse the incoming request so that i can write logic to set analog pins to certain values.

Example of goal (Or something similar):

char* parsedtext[] = ["true","1000","100","100","100"];

The thought behind this is that then i can go into index above and grab the value i want. I know how the data is structured so it can be hardcoded, i dont need to search.
My code is as follows:
*void setup() {

void loop() {
char incoming[84] = “GET /ajax_inputs?freqOn=true&freq=1000&RGBred=100&RGBgreen=100&RGBblue=100 HTTP/1.1”; // Length of data i want: 74
char HTTP_req[100];
char parsed_HTTP_req[100];

// Remove " HTTP/1.1" from char array. NOTE: Need to know length of data we want.. This changes with more flags in GET req..
int datalength = 74;
strcpy(HTTP_req,incoming); // Copy string from incoming to HTTP_req in order to maintain original request

for(int i = strlen(incoming); i >= datalength; i--){
	HTTP_req[i] = '\0'; // Insert '\0' at end to clean up.

char* split1;
split1 = strtok(HTTP_req, "?"); // tokenize HTTP_req until we hit "?"

char* flag1;
char* flag2;
char* flag3;
char* flag4;
char* flag5;

int flagCounter = 1; // Counter to keep track of which flag we want to use below

while(split1!=NULL) { // Keep going until we hit NULL
	for(int i = 0; i < 6; i++){ // For-loop in order to clean up some more
		if(i == 0){ // Remove "GET /ajax_inputs" by setting current token to NULL
			split1 = strtok(NULL, "&"); 
		else { // Else continue with parse, save to pointers for further parsing.
			switch (flagCounter){
				case 1:
					flag1 = split1;
				case 2:
					flag2 = split1;
				case 3:
					flag3 = split1;
				case 4:
					flag4 = split1;
				case 5:
					flag5 = split1;
			// Used for debug to see results.

			Serial.print("Flag1 : ");
			Serial.print("Flag2 : ");
			Serial.print("Flag3 : ");
			Serial.print("Flag4 : ");
			Serial.print("Flag5 : ");
			split1 = strtok(NULL, "&");

* *My reasoning for above code is that i want to parse out the information i need in order to manipulate the Arduino, but i had troubles using the strtok() function. I dont think i fully understand how it works, even after spending hours reading online references and examples.* *The output from above code (when flag1-5 is set) is:* *
Flag1 : freqOn=true
Flag2 : freq=1000
Flag3 : RGBred=100
Flag4 : RGBgreen=100
Flag5 : RGBblue=100

I have tried to include all relevant information in the post, if something is unclear please let me know and i will try to make it clear.
I would be extremely gratefull if someone would help me out with this.
Thanks for reading!

Hard code a GET request.

Learn how to parse it.

How do you know that the GET request is NULL-terminated? strtok won't work without it.

I have hardcoded the GET request in the above code in order to make a parser.

As i tried to explain, i am really trying to learn how to parse it, but i am stuck and thus i came here to see if anyone was able to help me.

I know that the GET request is NULL-terminated because im doing that in my code:

    for(int i = strlen(incoming); i >= datalength; i--){
        HTTP_req[i] = '\0'; // Insert '\0' at end to clean up.

Your having the same problem I had yesterday… I tried creating something to do what your needing and failed… I posted here and got incredible results with code and everything :slight_smile:

Take a look:
Parsing string IOT project…