bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.7k stars 1.12k forks source link

ArduinoJson parsing fail - Invalid Input #1160

Closed Nocturnalecode closed 4 years ago

Nocturnalecode commented 4 years ago

Hello, i recieve "Invalid Input" error always Can you check my code if anything wrong? Trying to debug it with prints but returning json seems correct and looks like this:

{
    "transmitterHost": "ESP_0EC076",
    "pairingCode": "625006a517cc4eb9b57d95a9654ba5e7"
}

When i print payload to serial port screen i see this line:

{"transmitterHost":"ESP_0EC076","pairingCode":"625006a517cc4eb9b57d95a9654ba5e7"} But parsing fails.

    String url = "http://"+host+"/Register/receiver?receiverHost="+hostStr;
    Serial.print("[HTTP] begin...\n");
    http.begin(url);  // HTTP
    Serial.println(url);

      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();
      Serial.println("[HTTP] GET DONE...\n");

          Serial.println("Trying to read http.getString()!");
          String payload = http.getString();
          Serial.println("String payload = http.getString() ok!");
          Serial.print("Incoming json: ");
          Serial.println(payload);

      std::string json = payload.c_str();

      Serial.println("");

      Serial.println("trying to create doc!");
      StaticJsonDocument<200> doc;
      Serial.println("DONE with create doc!");
      Serial.println("Trying deserializaJson...");
      DeserializationError error = deserializeJson(doc, json);

      if(error){
        Serial.print("deserialize fail, code: ");
        Serial.println(error.c_str());
        return;
      }

Also it's takes so long to get jsonstring (about 40 seconds). Using ESP8266HTTPClient library for that. If problem is that, have any advice for getting json? or if anythn else, have any idea?

Nocturnalecode commented 4 years ago

I have figured that occurs on my office's internet connection but works good at home and figured something that json comes like this at home;

22:33:31.234 ->{"transmitterHost":"","pairingCode":"2366dfe831104da0aff6bb9769074fa8"}

but at office it comes like;

09:59:37.230 -> 47 09:59:37.230 -> {"transmitterHost":"","pairingCode":"2366dfe831104da0aff6bb9769074fa8"} 09:59:37.230 -> 0 09:59:37.230 -> 09:59:37.230 ->

have 47 before object, 0 and two whitespace after object. is there a way to ignore those characters and just start from "{" and end at "}" ?

ewaldc commented 4 years ago

@Nocturnalecode, that is a HTTP 1.1 chunked response. The 47 means that the next chunk contains 47 bytes, and the 0 means "end of transmit".
But according to the ESP Arduino source code (from your code I assumed you are working with ESP 8266/32) on my system both HTTPClient::getString() and HTTPClient::writeToStream() should be able to recognize and handle these.

However it may be possible that I fixed a defect in the ESP Arduino HTTPClient library and forget to post a PR. Will go back and check and compare my version with the latest master.

Are you using the master branch on GitHub or a specific version? Ewald

Nocturnalecode commented 4 years ago

@ewaldc i am an alien for github actually trying to get used to. By changing connection it works well, not sure why that occurs on office's connection. Http getstring takes too long to get a response in office also. Might be rooter or service provider. Don't know.

bblanchon commented 4 years ago

Hi @Nocturnalecode,

I agree with @ewaldc, ESP8266HTTPClient should handle chunked transfer encoding for you. I don't know why it doesn't work; you should ask the authors of the library.

By the way, I see that your program is duplicating the response several times. Please read How to use ArduinoJson with ESP8266HTTPClient? to learn how to improve your program.

Best regards, Benoit

bblanchon commented 4 years ago

Hi @Nocturnalecode,

It just came to my attention that ESP8266HTTPClient doesn't handle chunked transfer encoding when we use getStream(), so you must call useHTTP10(true) before sending the request. I updated the documentation accordingly.

Best regards, Benoit

Nocturnalecode commented 4 years ago

@bblanchon @ewaldc thanks for help guys.

bblanchon commented 4 years ago

You're welcome @Nocturnalecode. Thank you for using ArduinoJson.