title: "Unexpected token in JSON at position 0" using JSON.parse in Node with valid JSON
body: <p>I've been tearing my hair out over this one for hours now.</p>
  
  <p>I have a simple Node server that's making a call to an external API to get a (massive, like 4+ MB) bit of JSON. I'm using about as boilerplate a request as you can get, taken straight from the Node docs:</p>
  
  <pre><code>const muniURL = `http://api.511.org/transit/vehiclemonitoring?api_key=${API_KEYS.API_KEY_511}&amp;format=json&amp;agency=sf-muni`;
  
  http.get(muniURL, (res) =&gt; {
    const statusCode = res.statusCode;
    const contentType = res.headers['content-type'];
    console.log('Status Code:', statusCode);
    console.log('Content Type:', contentType);
  
    let error;
    if (statusCode !== 200) {
      error = new Error(`Request Failed.\n` +
                        `Status Code: ${statusCode}`);
    } else if (!/^application\/json/.test(contentType)) {
      error = new Error(`Invalid content-type.\n` +
                        `Expected application/json but received ${contentType}`);
    }
    if (error) {
      console.log(`Request error: ${error.message}`);
      // consume response data to free up memory
      res.resume();
      return;
    }
  
    res.setEncoding('utf8');
    let rawData = '';
    res.on('data', (chunk) =&gt; rawData += chunk);
    res.on('end', () =&gt; {
      try {
        const parsedData = JSON.parse(rawData);
        console.log('parsedData:', parsedData);
      } catch (e) {
        console.log(`Caught error: ${e.message}`);
      }
    });
  }).on('error', (e) =&gt; {
    console.log(`Got error: ${e.message}`);
  });
  </code></pre>
  
  <p>...and every single time, it hits the <code>catch</code> statement with:
  <code>Caught error: Unexpected token  in JSON at position 0</code>. (Note the two spaces between 'token' and 'in'.)</p>
  
  <p>I've checked the JSON returned from both Chrome and Postman with two different web-based JSON validators, and it comes back as valid. While writing <code>rawData</code> to a file looks something like a buffer(?)...</p>
  
  <pre><code>1fef bfbd 0800 0000 0000 0400 efbf bdef
  bfbd efbf bd72 efbf bdc8 b62d efbf bd2b
  0c3f 7547 1cef bfbd 00ef bfbd efbf bd0b
  efbf bd5b 49ef bfbd 2def bfbd 6c6b efbf
  bd5c 55ef bfbd efbf bd44 3fef bfbd 126c
  71ef bfbd 021c 2029 6def bfbd 13ef bfbd
  efbf bdef bfbd 437f 52ef bfbd 4227 48ef
  bfbd efbf bd4d efbf bd31 13ef bfbd 09ef
  bfbd 5d2f 7bef bfbd efbf bde5 aa81 745e
  efbf bd65 efbf bd31 efbf bdef bfbd efbf
  ...
  </code></pre>
  
  <p>...<code>Buffer.isBuffer</code> comes back false.</p>
  
  <p>Thus far I've tried <code>JSON.stringify</code>ing first, <code>toString</code>ing, converting to a <code>new Buffer</code> and then stringifying, <code>.trim</code>ing white space, and <code>replace</code>ing all sorts of escaped characters, all to no avail.</p>
  
  <p>What am I missing here?</p>
  
  <hr>
  
  <p>EDIT: I realized that I was validating JSON fetched by Chrome and Postman, which apparently are doing some pre-processing of some sort. <code>curl</code>ing the URL yields a whole bunch of mess that's definitely not JSON. Still left with the questions of what data type that mess actually is, and why I'm not getting JSON when I'm specifically requesting it.</p>