title: Saving a XLSX file from a HTTP API server, via Javascript
body: <p>I have a http service which returns a HTTP response of type </p>
  
  <pre><code>Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  </code></pre>
  
  <p>and if I call this HTTP service from curl and redirect I get the .xlsx file recreated correctly. However trying to save the file from javascript fails. The code that I am using:</p>
  
  <p>cURL:</p>
  
  <pre><code>$ curl -v -X POST &lt;API_ENDPOINT&gt; &gt; a.xlsx ;# a.xlsx works fine
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>$http.post(&lt;API_ENDPOINT&gt;).then(function(response) {
            console.log(response);
            downloadFile(response.data, "b.xlsx")
  });
  
  var downloadFile = function(responseData, fileName) {
        var blob = new Blob([responseData], {
          type: 'application/vnd.ms-excel'
        });
  
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveBlob(blob, fileName);
        } else {
          var a = document.createElement('a');
          var url = window.URL.createObjectURL(blob);
  
          document.body.appendChild(a);
          a.style = 'display: none';
          a.href = url;
          a.download = fileName;
          a.click();
          window.URL.revokeObjectURL(url);
          document.body.removeChild(a);
        }
  };
  </code></pre>
  
  <p>The file is saved, however the contents seem to be different from the curl one, even though the server is sending the same file.</p>
  
  <p>The <code>content-length</code> header value is the same for both the responses (curl and javascript (captured via chrome devtools)) but the filesize when redirected via curl is 5.1k (almost same as the <code>content-length</code> value) but the the filesize of file created via js, is 8.5k and is wrong.</p>
  
  <p>I've tried setting <code>application/octet-stream</code>, <code>octet/stream</code>, <code>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</code> while creating the blob and none of that helps.</p>
  
  <p>I think I am messing up something in content-type and the blob creation but not able to figure out what is wrong. Any help ?</p>