Embedded Geek Embedded Geek - 4 months ago 9
C Question

Parse the HTTP String

How can I parse this string

GET /STA/ID=HelloWorld/Pass=Testin123 HTTP/1.1
First I need to check for
STA
, if it exists, continue to scan the string. Put Value of
ID
in this case
HelloWorld
should be store in char data type
SSID
and Value of
Pass
, in this case
Testin123
should be store in char data type
Pass


It should confirm the presence of
STA
in string first. If it is not present, do not enter into loop. If it exits, search for
ID
and
Pass
. Store it.

Now the problem is I am unable to store value of
ID
and
pass
. Also unable to search for
STA
.

char GetString[] = "GET /STA/ID=Test/Pass=123 HTTP/1.1";

char *get = strtok(GetString, " ");
char *request = strtok(NULL, " ");
char *rtype = strtok(NULL, " ");
char *FirstPart;

int main()
{
if (request != NULL)
{
FirstPart = strtok(request,"/");
while(FirstPart)
{
if (!strncmp(part, "STA"))
{
//Print STA Found

if(!strncmp(part, "ID=", 3))
{
//Store value of ID
}

if(!strncmp(part, "Pass=", 5))
{
//Store the Pass
}
}
}
FirstPart =strtok(NULL,'/');
}
}

Answer

Needed a bit of clean-up. One hint: switch all warnings and errors on with your compiler, they exist for a reason. Your code didn't even compile and that is the minimal condition here.

But nevertheless:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char GetString[] = "GET /STA/ID=Test/Pass=123 HTTP/1.1";
  // you cannot do it globally in that way, so I pulled it all into main()
  char *request, *FirstPart;
  // please don't use all upper-case for normal variables
  // I did it for some clarity here
  char *ID, *PASS;

  // skip "GET"
  strtok(GetString, " ");
  // get first part
  request = strtok(NULL, " ");

  if (request != NULL) {
    FirstPart = strtok(request, "/");
    // check for base condition
    if (!strncmp(FirstPart, "STA", 3)) {
      //Print STA Found
      printf("STA = %s\n", FirstPart);
    } else {
      fprintf(stderr, "STA not found!\n");
      exit(EXIT_FAILURE);
    }
    FirstPart = strtok(NULL, "/");
    // graze the key-value combinations
    while (FirstPart) {
      // We check them all here, one after the other
      if (!strncmp(FirstPart, "ID=", 3)) {
    //Store value of ID
    ID = strchr(FirstPart, '=');
    // ID is now "=Test", so skip '='
    ID++;
    printf("ID = %s, value of ID = %s\n", FirstPart, ID);
      } else if (!strncmp(FirstPart, "Pass=", 5)) {
    //Store the Pass
    PASS = strchr(FirstPart, '=');
    // PASS is now "=123", so skip '='
    PASS++;
    printf("PASS = %s, value of PASS = %s\n", FirstPart, PASS);
      } else {
    printf("Unknown part \"%s\", ignoring\n", FirstPart);
      }
      FirstPart = strtok(NULL, "/");
    }
  } else {
    fprintf(stderr, "No input at all\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}

The pointers ID and PASS point to the null-terminated values only, they are not independent memory. You might use malloc() to get some and measure the amount with strlen(). With ID as the example: ptr_to_mem_for_ID = malloc(strlen(ID));