A blank PDF is returned when a POST request is executed, but works fine with GET

I have an AngularJs interface and a NodeJS database. I am trying to create a PDF on NodeJS based on some parameters from AngularJS and return the PDF in response.

The code I use works fine when I make a GET request from AngularJS, but it returns an empty PDF when I make a POST request. I need to make a POST request since I have to send some specific data.

First, I save the file to disk, and then send it to the external interface so that I can see that the PDF is being created correctly. This is either sent incorrectly or read correctly on FrontEnd.

Below is my AngularJS code

var url = {{My Node Server URL}};
            (Note: Works when I make a Get request, without sending post params)
            var $promise = $http.post(encodeURI(url), {
                    responseType: 'arraybuffer',
                    competitors: competitors,
                    channels: channels
            });

            $promise.then(

                function success(response) {


                    var blob = new Blob([response.data], { type : 'application/pdf' });
                    var pdfLink = (window.URL || window.webkitURL).createObjectURL( blob );

                    window.open(
                      pdfLink,
                      '_blank' // <- This is what makes it open in a new window.
                    );

                    cb(pdfLink);

              }, function error(response) {

                --Error Handling--
              }
           )

Below is my NodeJS code

wkhtmltopdf(
            html,
            { 
                output: pdfName
            },
            function (code, signal) {
                fs.readFile(pdfName, function (err, data) {
                    if (err) {res.writeHead(400); res.end("" + err); return;}

                    res.writeHead(
                        200,
                        {
                            "content-type" : "application/pdf",
                            "Content-Disposition": "attachment; filename=Best.pdf "
                        }
                    );
                    res.end(data);
                });     
            }
        );

, . // . , , .

+4
2

! responseType arraybuffer . , , . , !

var $promise = $http.post(encodeURI(url), {
                    competitors: competitors,
                    channels: channels
            },{responseType: 'arraybuffer'});
+7

, , Angular 2+, , -. .: https://angular.io/docs/ts/latest/api/http/index/ResponseContentType-enum.html

-, res.download(filename).

:

downloadDoc(docName: string) {
  let pkg = {docName: docName};
  let opts = new RequestOptions({
    responseType: ResponseContentType.ArrayBuffer
  });
  return this.http
          .post(this.baseUrl + '/downloadDoc', pkg, opts)
          .toPromise()
          .then((res: any) => {
            let blob = new Blob([res._body], {type: 'application/pdf'});
            return blob;
          });
}
+2

All Articles