I have a node application which needs to do the following:
- POST a request with some HTML in the body to a PHP API that returns a PDF
- Return the PDF directly to the browser
I am trying something like this with node-fetch
:
const response = await fetch('https://php-pdf-api.test', {
method: 'post',
body: JSON.stringify({
'html': '<div>Hello</div>'
}),
header: {'Content-Type': 'application/json'}
})
const data = await response.text()
res.setHeader("Content-Type","application/pdf");
res.send(data)
Which results in a jumbled file:
I imagine something is happening to the encoding when using response.text()
, how can I pass this data on to the user's browser properly?