-->
Page 1 of 2

Lost on how to read client (JSON) data as a webserver?

PostPosted: Thu May 25, 2017 12:05 pm
by brenzo
I'm sure there's a really simple answer here and maybe I'm Googling for the wrong thing but I can't for the life of me figure out how to read data from the client when I'm the webserver. Just how to handle posts to specific URL's. For example...

Code: Select all    ESP8266WebServer server(80);
    server.on("/config", handle_config);

//////

    void handle_config(){
        //I want to intercept a json telegram from my client here
        //I will store it in a buffer and pass it to a JSON parsing function
    }

Re: Lost on how to read client (JSON) data as a webserver?

PostPosted: Thu May 25, 2017 12:39 pm
by martinayotte
The server receive the post as arguments, for example :
Code: Select all  if (server.args() > 0 ) {
    if (server.hasArg("password")) {
      pw = webserver.arg("password");
      Serial.println(pw);
    }

Re: Lost on how to read client (JSON) data as a webserver?

PostPosted: Thu May 25, 2017 1:25 pm
by Pablo2048
IMHO this can not be done in standard Arduino Core library. You have to use AsyncWebserver and hook receiving callback like this
Code: Select all  www.on("/saverulesxml", HTTP_POST, www_XMLrules, NULL, www_bodyXMLrules);

then www_bodyXMLrules (change the name :-) ) is invoked periodically as data arrive to webserver...

EDIT!!! - Maybe i'm wrong and You can handle this even in standard core - try to study HTTPUpload object... (anyway i'm using Async - it's faster, supports multiple connections and websockets in one port...)

Re: Lost on how to read client (JSON) data as a webserver?

PostPosted: Thu May 25, 2017 1:31 pm
by brenzo
martinayotte wrote:The server receive the post as arguments, for example :
Code: Select all  if (server.args() > 0 ) {
    if (server.hasArg("password")) {
      pw = webserver.arg("password");
      Serial.println(pw);
    }


Are these like the headers in a POST?
Pablo2048 wrote:IMHO this can not be done in standard Arduino Core library. You have to use AsyncWebserver and hook receiving callback like this
Code: Select all  www.on("/saverulesxml", HTTP_POST, www_XMLrules, NULL, www_bodyXMLrules);

then www_bodyXMLrules (change the name :-) ) is invoked periodically as data arrive to webserver...


That seems surprising, I can't imagine it's an exceptionally uncommon thing to want to do. :P Even if I could just get all of the raw data sent by the client I'm happy to parse it myself. Any way to just get EVERYTHING in the POST?