title: Download file from http post request - Angular 6
body: <p><strong>UPDATED with res.send(data) instead of res.json(data)</strong></p>
  
  <p>Using Angular 6 and NodeJS I am doing a web application.
  I am trying to download a file from a http post request.</p>
  
  <p>I send a request to the server like this. From my component I call a function in a service. In the component, I susbscribe to have the answer of the server and when I have it I create a new Blob with the response and I Use FileSaver to download the pdf. </p>
  
  <p>Now, when I received the answer from the server, the client sees it like an error whereas the status is 200. The error message is:
  <em>"Http failure during parsing for <a href="http://localhost:3000/api/experiment/regression" rel="noreferrer">http://localhost:3000/api/experiment/regression</a>"</em>
  See the screenshot below.</p>
  
  <p><strong>Component.ts</strong></p>
  
  <pre><code>this.api.postML(this.regression).subscribe(
      res =&gt; {
        console.log(res);
        let pdf = new Blob(res.data, { type: "application/pdf" });
        let filename = "test.pdf";
        FileSaver.saveAs(pdf, filename);
      },
      err =&gt; {
        alert("Error to perform the regression");
        console.log(err);
      }
    );
  </code></pre>
  
  <p><strong>API.Service.ts</strong></p>
  
  <pre><code>  public postML(data): Observable&lt;any&gt; {
      // Create url
      let url = `${baseUrl}${"experiment/regression"}`;
  
      let options = {
        headers: { "Content-Type": "application/json", Accept: "application/pdf" }
      };
      // Call the http POST
      return this.http
        .post(url, data, options)
        .pipe(catchError(this.handleError));
    }
  </code></pre>
  
  <p>Then from the server, it executes some code with the data sent and generates a PDF file.
  Then, I would like to send the pdf as a response to the client.
  I tried like this:</p>
  
  <pre><code>fs.readFile("/home/user/test.pdf", function(err, data) {
    let pdfName = "Report.pdf";
    res.contentType("application/pdf");
    res.set("Content-Disposition", pdfName);
    res.set("Content-Transfer-Encoding", "binary");
    console.log(data);
    console.log("Send data");
    res.status(200);
    res.send(data);
  });
  </code></pre>
  
  <p>In the client, I have the answer. The console log is:
  <a href="https://i.stack.imgur.com/M1uTt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/M1uTt.png" alt="console log"></a></p>