title: javascript ajax pdf download returns empty document
body: <p>I've a request that returns pdf file. It can be well displayed in a browser, downloaded by curl. </p>
  
  <p>My react frontend application call this request (ajax), and returns it to a user. </p>
  
  <p>This is how do I call it:</p>
  
  <pre><code>httpRequest({
      method: 'GET',
      responseType: 'blob',
      url: `/test/pdf?` +
              `param1=${var1}&amp;` +
              `param2=${var2}`
  });
  </code></pre>
  
  <p>This is httpRequest:</p>
  
  <pre><code>export const httpRequest = (options) =&gt; {
  return new Promise((resolve, reject) =&gt; {
      const contentType = options.contentType ? options.contentType : 'application/x-www-form-urlencoded';
      request.open(options.method, options.url, async);
      request.setRequestHeader('Content-Type', contentType);
      request.onreadystatechange = () =&gt; {
          if (request.readyState === readyState &amp;&amp; request.status === status) {
              switch (responseType) {
                  case 'json': {
                      if (request.responseText) {
                          resolve(JSON.parse(request.responseText));
                      } else {
                          throw new Error('Can not parse JSON!');
                      }
                      break;
                  }
                  case 'string': {
                      resolve(request.responseText);
                      break;
                  }
                  case 'blob': {
                      resolve(request.response)
                  }
                  default: {
                      resolve(request);
                      break;
                  }
              }
          }
      };
      request.send(data);
  });
  </code></pre>
  
  <p>};</p>
  
  <p>Having the response, I return it to a user:</p>
  
  <pre><code>const link = document.createElement('a');
  const file = new Blob([response.response], { type: 'application/pdf' });
  
  link.href = window.URL.createObjectURL(file);
  link.download = `test.pdf`;
  document.body.appendChild(link);
  link.click();
  </code></pre>
  
  <p>It returns me empty pdf (two empty pages). Its size in bytes is near 2 times bigger, than the normal pdf with content (I got it from curl). </p>
  
  <p>Here is typical difference between two pdfs (good is left, bad is right)
  <a href="http://storage5.static.itmages.com/i/18/0125/h_1516888730_3382943_f47d4f9cbe.png" rel="nofollow noreferrer">http://storage5.static.itmages.com/i/18/0125/h_1516888730_3382943_f47d4f9cbe.png</a> </p>
  
  <p>What is wrong in my code? Any workarounds? </p>