Fetch and download a PDF file with Axios and Javascript

Posted on October 23, 2021

Here the tip to download a binary file, such as a PDF, ZIP or image file with axios:

// within an async function
const res = await axios({
  responseType: 'blob',

// res.data will contains the binary data of the file

Now, if you want to force the download to the user browser, here the very popular trick:

const url = window.URL.createObjectURL(new Blob([res.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', 'myFile.pdf')