title: NodeJS, how to debug Parse Error at TLSSocket.socketOnData on GET request?
body: <p>I'm quite new to NodeJS and have been trying it out on AWS Lambda writing a simple Alexa service to tell me the temperature in my house. I've hit an issue that has me stuck - when I try to communicate with an https endpoint to query some data for a home automation thermostat - my Node application terminates with an error:</p>
  
  <pre><code>Error: Parse Error
      at TLSSocket.socketOnData (_http_client.js:362:20)
      at emitOne (events.js:96:13)
      at TLSSocket.emit (events.js:188:7)
      at readableAddChunk (_stream_readable.js:176:18)
      at TLSSocket.Readable.push (_stream_readable.js:134:10)
      at TLSWrap.onread (net.js:551:20)
  </code></pre>
  
  <p>This is a fairly simple GET request via https, and the providers of the endpoint have no issues using Python. I tried moving my code to a local Node setup on my laptop and am still having the problem (so its not AWS). As it's https and a service outside of my contral I can't easily wireshark the traffic to spot if the response is corrupt in some subtle way - however I tried using Browsify to convert the code to run in a web browser (where I can use wireshark) and of course it then works, and I can see nothing odd about the json response I then get.</p>
  
  <p>I've tried using the response module instead of the core https libraries in node but I still get the same underlying error.</p>
  
  <p>I'm stuck about what I can do - and have no clue how to debug this. It strikes me as either a formatting issue or a timing issue - but i'm not sure how to approach this. Ideas anyone?</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>var https = require('https');
  var crypto = require('crypto');
  
  function executeHttpRequest(path, data, method, username, password) {
  
      var encodedLogin = getLoginString(username,password);
      var stringData = JSON.stringify(data);
  
      var requestOptions = {
          host: 'hub-server.domain.co.uk',
          port: 443,
          path: path,
          method: 'GET',
          headers: {
              'Cache-Control': 'no-cache',
              'Accept': 'application/json, text/plain, */*',
              'Authorization': 'Basic ' + encodedLogin
          }};
  
      var request = https.request(requestOptions, function(response) {
  
          var body='';
          response.setEncoding('utf8');
  
          response.on('error', function(err){
              console.log(method + " error: \n", err.stack);
          }); 
  
          response.on('data', function (chunk) {
              console.log("read data: " + chunk);
              body += chunk 
          });
  
          response.on('end', function () {
              console.log("end data");
          });
  
      });
  
      request.on('error', function(err) {
          console.log('request error: \n' + err.stack);
      });
  
      console.log("sending request:" + JSON.stringify(requestOptions));
      console.log("with data: " + stringData)
  
      request.write(stringData);  
      request.end();
      console.log("closed request")
  }
  </code></pre>
  
  <p>When run in node I get the following output</p>
  
  <pre><code>sending request:{"host":"hub-server.domain.co.uk","port":443,"path":"/zone/1","method":"GET","headers":{"Cache-Control":"no-cache","Accept":"application/json, text/plain, */*","Authorization":"Basic bWFjdGEyOmY0OWZiODJlYTcyNWYzNDgwMTViODVjZTNlZmIyYmY1MWE4ZmI3YWUxMzJmZTRkNDk4OGEc"}}
  closed request
  request error: 
  Error: Parse Error
      at TLSSocket.socketOnData (_http_client.js:362:20)
      at emitOne (events.js:96:13)
      at TLSSocket.emit (events.js:188:7)
      at readableAddChunk (_stream_readable.js:176:18)
      at TLSSocket.Readable.push (_stream_readable.js:134:10)
      at TLSWrap.onread (net.js:551:20)
  </code></pre>
  
  <p>I get the same error with a similar PUT command. Interestingly if I miss out a parameter when using PUT, and the server replies with a JSON error, the above code works and show me JSON documenting the error. Similarly, if I use an invalid URL or bad AUTH I correctly get an appropriate status code. </p>
  
  <p>My code also seems to work if I trying hitting <a href="https://www.google.com" rel="nofollow noreferrer">https://www.google.com</a> and do a search.</p>
  
  <p>Obviously something is up - but how can I either correct my node code, or advise the authors of the endpoint?</p>