Edit : not a duplicate of Piping remote file in ExpressJS . In the referenced question, the distant file is accessible through an url and a GET
method. This is not the case here, I am receiving the file in a response payload after a POST
request.
I have a frontend that allows the user to download a document. For unrelated reasons, this document is generated by an external service each time the user requests it (based on some contextual data). So the pipeline is like this:
- User click link in front end
- Front-end send
GET
request to back-end - back-end get relevant internal data and send a
POST
request to external API - External API sends a response containing the doc itself, with appropriate
content-disposition=attachment
andcontent-type
headers, and the document in the body of the response. - My back-end forwards this to the front-end
- User is prompted with a download link
Steps 1 through 4 work. What my problem is:
- The user is prompted with a download popup, the file is of the right type (pdf in my example) but the pdf file is an empty page
- If I send the same request as my backend to the external API but through postman, I get the file I want with the right content.
- If I send the same request as my frontend to the backend but through postman, I get also get an empty pdf file.
My guess is I am not forwarding correctly the response from the external API to the front-end.
My code :
function getDocFromExternalApi(req, res) {
sendMyRequest().then(externalApiResponse => {
res.status(200).header({ //headers appears to be what is expected
'content-disposition': externalApiResponse.headers['content-disposition'], //= 'attachement; filename="someFileName.pdf"'
'content-type': externalApiResponse.headers['content-type'] // = 'application/pdf'
}).send(externalApiResponse.data) //externalApiResponse.data contains the file
})
}
I also tried adding the charset to content-type
and an appropriate content-length
header, but no dice either.
The full response from the external API : https://pastebin.com/5FX72eAX