'node js - Download pdf via HTTP post and upload to azure blob storage

I've an online API that returns PDF files via HTTP post requests. Once I get back the response with the file I would like to upload the file in an azure blob storage.

I've tried everything on here and I'm unable to get it work.

Where I'm now:

  async function getPDFfile(idSession) {
        let connectionJson = {
            "DeviceId": "device-id",
            "SessionId": idSession,
            "ContainerId": contaierID
        }

        axios({
            url: 'https://URL/exportPDF/',
            method: 'POST',
            Headers: connectionJson,
            responseType: "arraybuffer",
            responseEncoding: "binary"
        }).then((response) => {
            console.log(response.data)
         });

If I print this I will get a <Buffer but as I cannot really see what's Inside I cannot use it.

If I use axios without params:

        const response = await axios
            .post('https://URL/exportPDF/', connectionJson)

        return response.data

Here I get loads of unicode characers with some information, I've tried to upload this but the PDF file is only few bytes and obviously does not work.

I've tried to get the response as blob but same, was not working.

Could you please help me with figure this out?

this should run on azure function. Thank you for your time.

---- Edit

While the first method doesn't return anything in the data, the second without parameters returns a very long string with:

%PDF-1.4
%´┐¢´┐¢´┐¢´┐¢
1 0 obj
<<
/CreationDate(D:20220423222622+01'00')
/Creator(empira MigraDoc 1.50.5147 \(www.migradoc.com\))
/Title(TrustID)
/Producer(PDFsharp 1.50.5147-gdi \(www.pdfsharp.com\))
>>
endobj
2 0 obj
<<
/Type/Catalog
/Pages 3 0 R
>>
endobj
3 0 obj
<<
/Type/Pages
/Count 3
/Kids[4 0 R 23 0 R 28 0 R]
>>
endobj
4 0 obj
<<
/Type/Page
/MediaBox[0 0 841.89 595.276]
/Parent 3 0 R
/Contents 5 0 R
/Resources
<<
/ProcSet [/PDF/Text/ImageB/ImageC/ImageI]
/ExtGState
<<
/GS0 6 0 R
/GS1 10 0 R
>>
/XObject
<<
/I0 9 0 R
>>
/Font
<<
/F0 14 0 R
/F1 18 0 R
/F2 22 0 R
>>
>>
/Group
<<
/CS/DeviceRGB
/S/Transparency
>>
>>
endobj
5 0 obj
<<
/Length 2094
/Filter/FlateDecode
>>
stream
x´┐¢´┐¢Y╦Ä´┐¢´┐¢´┐¢W´┐¢L:##´┐¢B´┐¢´┐¢´┐¢´┐¢Cw$´┐¢´┐¢~´┐¢´┐¢´┐¢╚¬´┐¢v´┐¢´┐¢´┐¢´┐¢3´┐¢*´┐¢8q"´┐¢´┐¢´┐¢´┐¢Io´┐¢´┐¢´┐¢´┐¢O´┐¢´┐¢´┐¢No?´┐¢qz´┐¢E´┐¢4'[^´┐¢´┐¢(Ì£W>´┐¢´┐¢9&´┐¢´┐¢´┐¢w█½´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢█ôVA´┐¢´┐¢O´┐¢´┐¢
>´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢~´┐¢S´┐¢´┐¢  ´┐¢´┐¢´┐¢█À_´┐¢K(_bt┬╣´┐¢´┐¢´┐¢!´┐¢´┐¢;9V&´┐¢G´┐¢´┐¢´┐¢;=´┐¢´┐¢´┐¢G´┐¢═┐]´┐¢E┌│Ì░´┐¢´┐¢[´┐¢´┐¢´┐¢fs´┐¢(p´┐¢╦Å´┐¢Z´┐¢´┐¢´┐¢>´┐¢´┐¢¤ô´┐¢.|´┐¢´┐¢´┐¢r´┐¢NQ ´┐¢Èû´┐¢´┐¢´┐¢)´┐¢d5´┐¢?´┐¢W´┐¢´┐¢´┐¢´┐¢´┐¢.:´┐¢$´┐¢´┐¢X´┐¢si´┐¢´┐¢´┐¢´┐¢´┐¢C´┐¢´┐¢´┐¢´┐¢Z5´┐¢j´┐¢U´┐¢Lr´┐¢qw3╚½3m)´┐¢>´┐¢´┐¢´┐¢Ðí´┐¢´┐¢´┐¢Yy´┐¢,/B´┐¢´┐¢´┐¢u´┐¢qW´┐¢ÐÅ´┐¢Ôö¿´┐¢E;´┐¢´┐¢ k´┐¢m=´┐¢´┐¢"´┐¢k´┐¢´┐¢´┐¢}´┐¢´┐¢m´┐¢´┐¢´┐¢\´┐¢*´┐¢y{´┐¢V=´┐¢%´┐¢8´┐¢´┐¢´┐¢k !D´┐¢´┐¢KGM´┐¢´┐¢´┐¢i´┐¢&´┐¢´┐¢
´┐¢´┐¢´┐¢´┐¢J*Y´¢ºX"6´┐¢|[j´┐¢´┐¢´┐¢´┐¢*´┐¢z)´┐¢´┐¢´┐¢>n&,w6´┐¢e´┐¢´┐¢5`Av´┐¢´┐¢´┐¢\ZD´┐¢e´┐¢I´┐¢´┐¢´┐¢´┐¢@´┐¢R ´┐¢a´┐¢´┐¢h2W´┐¢´┐¢´┐¢´┐¢´┐¢a´┐¢´┐¢´┐¢diT}T 3´┐¢,´┐¢il´┐¢´┐¢L´┐¢t}´┐¢E´┐¢´┐¢´┐¢v´┐¢´┐¢~f´┐¢R╚ôF0´┐¢´┐¢╔û
Su´┐¢´┐¢´┐¢36´┐¢´┐¢x´┐¢b´┐¢´┐¢´┐¢,´┐¢j´┐¢´┐¢v2R┘ÉÍô}W%´┐¢`F)´┐¢´┐¢c%´┐¢b´┐¢´┐¢´┐¢´┐¢)´┐¢6´┐¢/my$"5´┐¢\▄ƒ´┐¢T<´┐¢EN´┐¢´┐¢´┐¢´┐¢gXo7´┐¢´┐¢f´┐¢´┐¢
ng´┐¢╬ɦñnD|b}´┐¢¦░P´┐¢´┐¢´┐¢$($´┐¢Èæ´┐¢´┐¢r1
´┐¢═Æ´┐¢`´┐¢´┐¢c´┐¢´┐¢h´┐¢ ](9´┐¢´┐¢´┐¢´┐¢´┐¢AßÜé´┐¢J>:´┐¢´┐¢u$`´┐¢E´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢P´┐¢^0´┐¢´┐¢8h´┐¢´┐¢´┐¢8  g´┐¢´┐¢%zD´┐¢´┐¢´┐¢´┐¢7yRib´┐¢S´┐¢]´┐¢´┐¢´┐¢´┐¢´┐¢k´┐¢´┐¢A@´┐¢´┐¢´┐¢´┐¢-µ▒╣´┐¢´┐¢k´┐¢B´┐¢(:.´┐¢´┐¢)´┐¢po<´┐¢´┐¢Q´┐¢´┐¢0 ´┐¢´┐¢=*#Z´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢<Bs╩ö´┐¢_r   ´┐¢g´┐¢G´┐¢db´┐¢6GT´┐¢´┐¢´┐¢´┐¢,´┐¢┘ÿ%´┐¢zy´┐¢´┐¢UD´┐¢e´┐¢1l´┐¢´┐¢q╬è´┐¢´┐¢9p´┐¢j´┐¢´┐¢´┐¢´┐¢]D´┐¢a)´┐¢´┐¢j´┐¢´┐¢9,´┐¢_9´┐¢%´┐¢c&´┐¢´┐¢´┐¢"z´┐¢´┐¢S=$´┐¢´┐¢´┐¢\▄ñ35´┐¢´┐¢´┐¢i´┐¢9Q´┐¢▀¢´┐¢´┐¢~´┐¢´┐¢_´┐¢´┐¢.´┐¢þ║║´┐¢´┐¢´┐¢^¤¡y´┐¢´┐¢iC´┐¢´┐¢´┐¢´┐¢´┐¢O´┐¢ÌÀn´┐¢m* ´┐¢´┐¢´┐¢u´┐¢kk´┐¢#´┐¢R´┐¢´┐¢tÈï W´┐¢´┐¢G(Í«h´┐¢´┐¢´┐¢D´┐¢´┐¢k>khX´┐¢%@´┐¢J]p´┐¢#´┐¢1´┐¢´┐¢ÈÑ´┐¢O´┐¢f´┐¢´┐¢´┐¢´┐¢gl´┐¢´┐¢´┐¢T~LF´┐¢´┐¢yG´┐¢=´┐¢-´┐¢´┐¢´┐¢╔×3A─¬L´┐¢´┐¢Zx´┐¢Jf´┐¢v´┐¢´┐¢´┐¢´┐¢:´┐¢´┐¢FH´┐¢nW1{lX´┐¢´┐¢ZYlF´┐¢´┐¢tPm:´┐¢*y´┐¢´┐¢.a´┐¢═×=´┐¢.´┐¢´┐¢´┐¢ ÃÄ{q´┐¢v´┐¢Y´┐¢0LE´┐¢´┐¢
yÎ▓´┐¢´┐¢´┐¢´┐¢1Tο´┐¢´┐¢k´┐¢RP´┐¢´┐¢w´┐¢;m´┐¢Da´┐¢A2´┐¢N´┐¢Xq´┐¢M´┐¢´┐¢´┐¢¤╝$´┐¢K´┐¢S´┐¢Ì╣9'´┐¢,U´┐¢├×´┐¢´┐¢"G´┐¢hWZ´┐¢´┐¢v─¼h´┐¢´┐¢EDr´┐¢`´┐¢Ae´┐¢ãÄ5$´┐¢´┐¢´┐¢Y´┐¢i´┐¢´┐¢e5´┐¢´┐¢(´┐¢8_
´┐¢2o´┐¢´┐¢´┐¢Pc´┐¢´┐¢) ´┐¢@´┐¢s´┐¢,2´┐¢">´┐¢´┐¢=)´┐¢´┐¢iM4j´┐¢´┐¢´┐¢O´┐¢´┐¢1
´┐¢´┐¢4´┐¢?´┐¢´┐¢R?{´┐¢´┐¢´┐¢´┐¢ÐÖCe´┐¢´┐¢µñ┐´┐¢_´┐¢´┐¢"}´┐¢´┐¢Ln´┐¢@TZK´┐¢´┐¢4´┐¢$´┐¢´┐¢"M´┐¢´┐¢´┐¢´┐¢JD´┐¢r$´┐¢´┐¢1G´┐¢´┐¢´┐¢´┐¢|´┐¢H2Îû´┐¢´┐¢´┐¢´┐¢´┐¢5G´┐¢´┐¢´┐¢@´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢O{<D´┐¢´┐¢#´┐¢;K´┐¢M´┐¢´┐¢╩©´┐¢a8SD6s´┐¢´┐¢´┐¢´┐¢ ´┐¢G´┐¢´┐¢f ´┐¢´┐¢´┐¢´┐¢whw!"#´┐¢´┐¢´┐¢-´┐¢3A´┐¢´┐¢q´┐¢´┐¢´┐¢/´┐¢`´┐¢]g´┐¢qi´┐¢´┐¢5O}N´┐¢(´┐¢´┐¢0  ´┐¢1´┐¢´┐¢´┐¢´┐¢h´┐¢´┐¢
Q´┐¢6├Ç´┐¢kU´┐¢´┐¢´┐¢!´┐¢g/(´┐¢´┐¢uq4´┐¢n´┐¢n´┐¢~´┐¢╔©UR'4´┐¢´┐¢;P´┐¢aK´┐¢5ð¢´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢ã¼R´┐¢´┐¢{´┐¢@7{´┐¢´┐¢t´┐¢¤╗J*´┐¢G´┐¢´┐¢´┐¢´┐¢´┐¢,:´┐¢´┐¢zB|´┐¢x´┐¢~-´┐¢´┐¢u´┐¢eH´┐¢vcwH"´┐¢c´┐¢´┐¢´┐¢   xC▄º´┐¢)O´┐¢´┐¢ny´┐¢yZEx´┐¢@e´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢}ÌÇ´┐¢´┐¢ZF0´┐¢fbT´┐¢´┐¢´┐¢´┐¢´┐¢.´┐¢Z(´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢╬í<´┐¢O|W´┐¢t!´┐¢´┐¢X=´┐¢U´┐¢'4$´┐¢´┐¢U6´┐¢´┐¢ ´┐¢w7f´┐¢$t´┐¢R´┐¢´┐¢­ƒëëI9´┐¢%!´┐¢═ë`G´┐¢´┐¢´┐¢´┐¢´┐¢y
´┐¢b´┐¢´┐¢´┐¢´┐¢´┐¢<L'A!´┐¢w%h=´┐¢´┐¢´┐¢Bom´┐¢´┐¢q´┐¢´┐¢~uP7Vg(c8Gt>´┐¢}´┐¢´┐¢xg ´┐¢t´┐¢´┐¢5ð¡z´┐¢(@´┐¢;!´┐¢▄ò´┐¢j´┐¢═ºk2c&´┐¢k´┐¢z´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢┬é´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢)´┐¢=´┐¢V´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢W\48´┐¢2´┐¢nG´┐¢´┐¢m´┐¢´┐¢ãü´┐¢Xs´┐¢´┐¢E*#´┐¢´┐¢#´┐¢Aj´┐¢´┐¢qJ╬┤c|w´┐¢´┐¢D´┐¢´┐¢´┐¢"7SËÿrL´┐¢u´┐¢1´┐¢´┐¢1´┐¢,]5´┐¢´┐¢´┐¢´┐¢ ´┐¢´┐¢´┐¢´┐¢´┐¢F´┐¢:´┐¢´┐¢\´┐¢´┐¢´┐¢$´┐¢m:q´┐¢6´┐¢´┐¢.}´┐¢  >0´┐¢´┐¢^╦À´┐¢HO´┐¢-´┐¢amO´┐¢´┐¢´┐¢E1´┐¢X╔äO´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢/´┐¢<´┐¢´┐¢V}'´┐¢ ´┐¢^´┐¢´┐¢Qqz@5´┐¢´┐¢´┐¢d/y´┐¢´┐¢´┐¢Ãë3Zi´┐¢´┐¢ G=i[/9
´┐¢G_´┐¢C´┐¢´┐¢|´┐¢3´┐¢T´┐¢´┐¢´┐¢´┐¢/´┐¢´┐¢´┐¢ZV´┐¢´┐¢A´┐¢´┐¢´┐¢´┐¢´┐¢´┐¢b´┐¢BaÌ×´┐¢´┐¢´┐¢´┐¢<

if I try to save the response.data in a file on my disk it creates a 3 pages pdf with the correct some data showing on the PDF tab reader but the pages are blank

        const response = await axios
            .post('https://APIURL/exportPDF/', connectionJson)
        //console.log(((response.data).toString()))

        fs.writeFile('c:/temp/my.pdf', response.data, (err) => {
            //     if (err) throw err;
            console.log('The file has been saved!');
        });
    };

as I've been reading that the file might not be completely downloaded I've tried:

        const finishedDownload = util.promisify(stream.finished);
        const writer = fs.createWriteStream('c:/temp/myfile.pdf');
        let connectionJson = {
             "Username": "",
            "Password": "",
        }
        const response = await axios({
            method: 'post',
            url: 'https://APIURL/exportPDF/',
            responseType: 'stream',
            headers: connectionJson
        });
    
        response.data.pipe(writer);
        await finishedDownload(writer);

but writes a file 0bytes.



Solution 1:[1]

Fixed it including responseEncoding:binary and option while writing the file 'binary' Please read the comments in the code!

 async function axaios(idSession) {
        
        let connectionJson = {
            "DeviceId": "device-id",
            "SessionId": idSession,
            "ContainerId": contaierID,
        }
        //important! this option is needed
        let conf = {"responseType": "arraybuffer",
        "responseEncoding": "binary"}


        const response = await axios
            .post('https://APIULR/exportPDF/', connectionJson, conf)
        //console.log(response.data)

        
        //'binary' needed as option!
        await fs.writeFile('c:/temp/my.pdf', response.data, 'binary' , (err) => {
            if (err) throw err;
            console.log('The file has been saved!');
        });
    }
    ```

Thank you all for your suggestions! 
Thanks a lot KJ

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1