fojas / cleverbot-node

Cleverbot client in node.js
MIT License
62 stars 33 forks source link

Unexpected token in JSON #36

Closed kraxarn closed 7 years ago

kraxarn commented 7 years ago

Sometimes, it seems like the JSON can't be parsed correctly and it then outputs the error SyntaxError: Unexpected token  in JSON at position x where x can be any number. Seems to be in between 1500 and 1800, but not sure.

I thought at first that this was because I ran out of API calls, but later the same day it can work just fine.

shantanuthatte commented 7 years ago

Same here.

SyntaxError
Unexpected token  in JSON at position 1826

Trace leads to line 55 at JSON.parse:

./node_modules/cleverbot-node/lib/cleverbot.js in Object.<anonymous> at line 55:35
            var chunks = [];
            res.on("data", function(data) {
              chunks.push(data);
            });
            res.on("end", function(i) {
              var body = Buffer.concat(chunks).toString();
              var responseBody;
              responseBody = JSON.parse(body);
              responseBody.message = responseBody.output; //for backwards compatibility
              this.state = responseBody.cs;
              cb(responseBody);
            }.bind(this));
        }.bind(this));
        req.end();
fojas commented 7 years ago

I reverted some code that should handle the bad chars in the response. There appears to be some issues with escape chars in JSON responses that I need to figure out. The fix I put in place is dirty, but it should work.