I have one Python API that returns a PDF file which when called directly from my terminal downloads the PDF fine and it all looks good when opened. However, I have a Nuxt web app supported by an Express backend which I want to use to open the PDF file in a new tab and to be downloadable. All I am getting in response from the Python API is a string which looks like this:
'%PDF-1.4\n' +
'1 0 obj\n' +
'<<\n' +
'/Title (��\x00F\x00l\x00o\x00o\x00d\x00S\x00m\x00a\x00r\x00t)\n' +
plus a few thousand more chars.
This string is what opens in the new tab. It looks like some encoded data, but I am not really sure which makes proceeding a bit harder.
I am assuming the Python API is fine, so I am thinking I need to do more with Express/Nuxt to make it work. The call to the Python API in Express is:
const response = await axios.get(
url, { headers: { "X-API-Key": process.env.FS_UPRN_API_KEY } }
);
res.setHeader("Content-type", "application/pdf")
res.setHeader('Content-Transfer-Encoding', 'binary');
res.setHeader('Accept-Ranges', 'bytes');
res.send(response.data)
In the Nuxt frontend:
this.$axios.get(`/api/uprn?uprn=${this.clickedUprn}`)
.then(response => {
window.open("", "_blank").document.write(response.data);
I have tried numerous configurations with different headers but haven't had any luck. How do I achieve the desired outcome?