1

I have a GET request that fetches what i think is xlsx data. If I paste the URL into Postman and click 'send and download' it makes the GET request and downloads the data into my Documents folder and I can easily open the xlsx file. How do I do that with javascript? I don't know what Postman is doing under the hood with their 'send and download' feature. But when I use Fetch in Javascript and get the data back, I'm not sure what to do with it. Below is what I get back from the API before I return it as res.text() because res.json() doesn't work

url: "https://xxxxx.com/get-xlsx"
redirected: false
status: 200
ok: true
statusText: ""
headers: Headers {}
body: ({ReadableStream})
bodyUsed: false```
  • 2
    Why you not just create the file (as .xlsx) on the server (much easier) and return it to the client (so your browser will download the file for you)? – thanhdx Feb 07 '20 at 02:14
  • I dont think I explained it very well, but that's exactly what I'm doing with the GET request. The server is sending an XLSX file, and its being returned to the client in the form of the code snippet I pasted. I just dont know how to make the client download it :( And that is what I'm really trying to ask is how to do that –  Feb 07 '20 at 02:56
  • If the server is sending an XLSX file, you can easily open the URL in a new tab and the browser will download it for you, or do it in JS by `window.open(URL)`. If the server is sending file content instead, that when you have to create new file with the content and download it. – thanhdx Feb 07 '20 at 03:50
  • The problem with window.open is that I have to send an auth token. So when i do window.open it fails because of auth –  Feb 07 '20 at 04:18
  • And again, when I use Postmans "send and download" button, it downloads the file right away. So I dont know what the 'send and download' button is doing in Postman, but whatever it is, it works –  Feb 07 '20 at 04:20
  • Have you already check this? https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch I guess it might help you. – Mart Macion Feb 07 '20 at 06:27

0 Answers0