Parse HTTP Get Request


how can parse string
code: [select]
get /sta/id=helloworld/pass=testin123 http/1.1
first need check sta, if exists, continue scan string. put value of id in case helloworld should store in string data type ssid , value of pass, in case testin123 should store in string data type pass.

it should confirm presence of sta in string first. if not present, not enter loop. if exits, search id , pass. store it.

the code used, seems not working.  doesn't enter loop, because can't search sta, , if does, not search id , pass

code: [select]

#include <esp8266wifi.h>
#include <esp8266webserver.h>

char getstring[100];

string ssid;
string pass;

string s = "http/1.1 200 ok\r\nconnection: close\r\ncontent-length: 0\r\n\r\n";

wifiserver server(80); //initialize server on port 80

void setup()
{
  serial.begin(115200); //start communication between esp8266-12e , monitor window
  wifi.mode(wifi_ap); //our esp8266-12e accesspoint
  wifi.softap("hello_iot", "12345678"); // provide (ssid, password); .
  server.begin(); // start http server

  ipaddress https_serverip = wifi.softapip(); // obtain ip of server
  serial.print("\n");
  serial.print("server ip is: "); // print ip monitor window
  serial.println(https_serverip);
}


void loop()
{
  wificlient client = server.available();
  if (!client) {
    return;
  }
  //looking under hood
  serial.println("somebody has connected :)");

  string incoming = client.readstringuntil('\r');
  serial.println(incoming);
  incoming.tochararray(getstring, 100);

  char *get = strtok(getstring, " ");
  char *request = strtok(null, " ");
  char *rtype = strtok(null, " ");

  serial.print("\n");
  serial.print("getstring :");
  serial.println(getstring);

  serial.print("\n");
  serial.print("request :");
  serial.println(request);

  serial.print("\n");
  serial.print("rtype :");
  serial.println(rtype);

  if (request != null)
  {
    char *part = strtok(request, "/");
 
    while (part)
    {
      if (!strcmp(part, "sta"))
      {
        if (!strncmp(part, "id=", 3))
        { // have id
          ssid = string(part + 3);
          serial.print("\n");
          serial.print("ssid: ");
          serial.print(ssid);
 
        }
 
        if (!strncmp(part, "pass=", 5))
        { // have password
          pass = string(part + 5);
          serial.print("\n");
          serial.print("pwd: ");
          serial.print(pass);
        }
      }
      else
      {
          serial.print("\n");
          serial.print("not found");
      }
      part = strtok(null, "/");
 
    }
  }
}

your code won't work don't progress parsing when find sta

your while loop find sta , if (!strncmp(part, "id=", 3)) fails because still point @ sta, , if (!strncmp(part, "pass=", 5)) fails because still point @ sta. arrive @ end of loop, next token part = strtok(null, "/"); , go check again agains sta... time it' no longer sta, it's id=... don't enter if clause because it's not sta...

long story short, don't parse string in right way...



Arduino Forum > Using Arduino > Programming Questions > Parse HTTP Get Request


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'