Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

node.js - Express proxy file download from 3rd party API

I have a case where my user is making a client-side POST request to download a file from my express server.

In this case, to get the file I have to make my own GET request to a 3rd party API. I am using Axios to make the GET request.

The API returns a response with the files content type and disposition:

Content-Type: application/pdf;charset=UTF-8

Content-Disposition: attachment;filename="/15935482_Addendum_copy.pdf"

The body of the response looks like this:

 %PDF-1.7 %???? 1 0 obj <> endobj 2 0 obj <> endobj 3 0 obj <> endobj 4 0 obj <>/ExtGState<>/Font<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/XObject<>>>/Type/Page>> endobj 5 0 obj <>/DR<<>>/F 132/FT/Sig/P 4 0 R /Rect[ 0 0 0 0]/Subtype/Widget/T(ENVELOPEID_9630045A3079429097C2D9D6D746968D)/Type/Annot/V 7 0 R >> endobj 6 0 obj <>/Subtype/Form/Type/XObject>>stream x? endstream endobj 7 0 obj </Filter/Adobe.PPKMS/Location()/M(D:20200610103851-07'00')/Prop_Build<>>>/Reason(Digitally verifiable PDF exported from www.docusign.com)/SubFilter/adbe.pkcs7.detached/Type/Sig>> endobj 8 0 obj <>/H/N/P 4 0 R /Rect[ 36.92 189.39 240.29 214.42]/Subtype/Link>> endobj 9 0 obj <>/H/N/P 4 0 R /Rect[ 37.55 136.82 240.92 161.85]/Subtype/Link>> endobj 10 0 obj <>/Border[ 0 0 0]/C[ 0 0 1]/Rect[ 95.37 780.56 162.52 786.59]/Subtype/Link>> endobj 11 0 obj <>/DR<<>>/F 132/FT/Sig/P 4 0 R /Rect[ 0 0 0 0]/Subtype/Widget/T(Signature1)/Type/Annot/V 13 0 R >> endobj 12 0 obj <>/Subtype/Form/Type/XObject>>stream x? endstream endobj 13 0 obj </Filter/Adobe.PPKLite/Location(United States)/M(D:20200610140239-04'00')/Prop_Build<>>>>/Reason(Digitally verifiable PDF exported from www.dotloop.com)/SubFilter/adbe.pkcs7.detached/Type/Sig>> endobj 14 0 obj <>stream x?+??| endstream endobj 15 0 obj <>stream x??R(T?? endstream endobj 16 0 obj <>stream x???????)JJ.f$???s??5?6??+?#,?a??B?Q?Ry?|

What I am doing is attempting to pass this data back to the client. Ideally, I would like to just trigger the browser to download the file, however, res. download() function accepts only a file path. I'm not sure what type of data the above is.

I've tried passing the data back to the client by copying the content type, disposition, and data.

app.post('/attachment', (req,res,next)=>{
    getAttachment(req.body.attachment.File_upload).then(response=>{
        res.setHeader('content-type', response.headers['content-type']);
        res.setHeader('content-disposition', response.headers['content-disposition']);
        res.send(response.data)
    }) 
})

This does appear to send the data back to the client, however, it does not trigger a download of the file.

Can anyone help explain what data type response.data is, that I'm receiving from the API, and what is the best way to download that response to the client?

question from:https://stackoverflow.com/questions/65944081/express-proxy-file-download-from-3rd-party-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...