title: JSON is cut short from HTTP response (Axios)
body: <p>I am using Axios in a Node.js (MERN stack) project to request data from the server. The result of the request is a large JSON string, but gets cut off prematurely. Strangely enough it sometimes works once if I start the backend and frontend servers fresh. The console output looks like this (someone please inline it, I dont have the reputation yet...):</p>
  
  <p><a href="https://i.imgur.com/9zts7IZ.png" rel="nofollow noreferrer">https://i.imgur.com/9zts7IZ.png</a></p>
  
  <p>On the backend:</p>
  
  <pre><code>app.route("/exercise/run").post(checkAuth ,function (req, res) {
      // doing some stuff (start child_process to run java program which returns json object)
      res.status(200).json(jsonObj);
  }
  </code></pre>
  
  <p>On the frontend:</p>
  
  <pre><code>let options = {
      timeout: 10000,
      maxContentLength: 1000000000,
  };
  Axios.post('http://localhost:4000/exercise/run', data, options)
      .then(response =&gt; {
          if (response.status === 200) {
              let output = Buffer.from(response.data).toString();
              let json = JSON.parse(output); // this fails because it is cut short
              console.log(json);
          }
      })
      .catch(function (error) {
          console.error(error);
      });
  </code></pre>
  
  <p>I kind of expect to have it work every time, but thats is not the case. I wonder if I dont close the connection correctly or prematurely, but I have been searching for hours now and have not found a working fix.</p>
  
  <p>I also tried to send it in chunks (even thought its not ment for that since I already know the size of the json and dont have to stream it), but that didnt work either. Same exact error, sometimes it works (usually when I restart everything), most of the time it gets cut off.</p>
  
  <p>On the backend:</p>
  
  <pre><code>function splitAndSendJSON(res, json) {
      const CHUNCK_SIZE = 2000;
  
      let jsonString = JSON.stringify(json);
  
      let dataArray = [];
      for (var i = 0; i &lt; jsonString.length; i += CHUNCK_SIZE) {
          dataArray.push(jsonString.slice(i, i + CHUNCK_SIZE));
          console.log(i);
      }
      res.writeHead(200, {
          'Content-Type': 'text/plain; charset=utf8',
          'Transfer-Encoding': 'chunked'
      });
      // res.write("[");
      for (var i = 0; i &lt; dataArray.length-1; i++) {
          res.write(dataArray[i]);
      }
      res.write(dataArray[dataArray.length-1]);
      // res.write("]");
      res.end();
  }
  </code></pre>
  
  <p>On the frontend:</p>
  
  <pre><code>let options = {
      timeout: 10000,
      responseType: 'stream',
      maxContentLength: 1000000000,
      headers: {
          'Connection': 'keep-alive',
          'Transfer-Encoding': 'chunked',
          'keepAlive':true,
          'maxSockets':1
      }
  };
  Axios.post('http://localhost:4000/exercise/run', data, options)
      .then(response =&gt; {
          if (response.status === 200) {
              if (typeof response.data === "string") {
                  let json= JSON.parse(response.data);
                  console.log(json)
              } else if (typeof response.data === "object") {
                  let output = Buffer.from(response.data).toString()
                  console.log(output);
                  let json= JSON.parse(output);
                  console.log(json)
              } 
          }
      })
      .catch(function (error) {
          console.error(error);
      });
  </code></pre>