Problem receiving JSON data by HTTP request and parse it

Hello guys,

I am running into an odd problem which I can’t solve on my own. Maybe you guys can and will help me out :smiley:

The main thing I am trying to achieve is to receive some data by HTTP POST request, and parsing it into some variables which can be used later.

Currently I have set up the following test sketch:

//===================================== Libraries toevoegen =============================================================================================================================================================================

#include <Ethernet.h>
#include <SPI.h>
#include <ArduinoJson.h>
//===================================== Globale Variabelen===============================================================================================================================================================================

String json = String(300);       

EthernetServer  server(80);            


//===================================== Ethernet var ====================================================================================================================================================================================

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED 
             };
 
IPAddress ip(192, 168, 1, 177);        
IPAddress myDns(192,168,1, 1);  
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

//===================================== Eenmalige setup==================================================================================================================================================================================
  void setup() {
//=====================================Pinmodes en overige inits=========================================================================================================================================================================
  
  Serial.begin(9600);                               
  
  Serial.println("IP Autodiscover");
  if (Ethernet.begin(mac) == 0) { 
                                  
    Ethernet.begin(mac, ip, myDns, gateway, subnet); 
  }
   Serial.print("Connect to me at: ");
   Serial.println(Ethernet.localIP()); 
  }

//===================================== Einde eenmalige setup==========================================================================================================================================================================
//
//===================================== Main Loop Begin ===============================================================================================================================================================================
void loop(){

 EthernetClient client = server.available();  
   if (client) {
     while (client.connected()) {
       if (client.available()) {

         char c = client.read();

       if (json.length() < 300) { 
         json.concat(c); } 
         
       Serial.print(c); 
       
       if (c == '\n') {  
          
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
  client.println();
  
StaticJsonBuffer<400> jsonBuffer;

JsonObject& root = jsonBuffer.parseObject(json); 


//   Test if parsing succeeds.
  if (!root.success()) {
   Serial.println("parseObject() failed");
  }
   else {

   Serial.println("parseObject() success!");
}

//clearing string for next read
json="";

//stopping client

client.stop(); 

Serial.println("Client stopped");

}}}}}

The function for receiving data in HTTP-requests works. I have recyled this code from earlier projects. The main difference is: i never used it in combination with JSON-data.

I am using the Chrome REST plugin to do some testing posts. The test JSON-payload i am using is:

{

	"hqid": "1",
	
}

As a result on the serial monitor (after posting) i get the following:

IP Autodiscover
Connect to me at: 192.168.1.121
POST /?%7B%0A%09%22hqid%22%3A+%221%22%2C%09%0A%7D HTTP/1.1
parseObject() failed
Client stopped

Obviously, the JSON- parsing fails because of the data not being in the right format. I can’t get this to work. Am I overlooking something?

Thank you!

Leon.

try to remove the comma from the json.

and if you want to read the 1 as number do not wrap it in ""

Juraj:
try to remove the comma from the json.

and if you want to read the 1 as number do not wrap it in ""

Thank you for replying!

I am aware that the JSON is not entirely in the correct format.

What concerns me the most is the readback from the post in the Serial monitor:

POST /?%7B%0A%09%22hqid%22%3A+%221%22%2C%09%0A%7D HTTP/1.1

I am expecting the posted document here (starting with { and so on..). The parser can not handle the data when it does start with /? and replaces al other characters with questionmarks and other numers...

EthernetServer is a TCP socket. you get what the browser sent. the HTTP POST line with requested url and the header lines. then an empty line and then the data. it is HTTP protocol