Skip to content

Instantly share code, notes, and snippets.

@NayabSiddiqui
Last active October 6, 2020 06:02
Show Gist options
  • Save NayabSiddiqui/bbfcaa6f1c90da9760af7a383bb42e2b to your computer and use it in GitHub Desktop.
Save NayabSiddiqui/bbfcaa6f1c90da9760af7a383bb42e2b to your computer and use it in GitHub Desktop.
Axios POST example to illustrate downloading of PDF from a REST api. Answer to the Stackoverflow question [Download binary file with Axios](https://stackoverflow.com/questions/49040247/download-binary-file-with-axios)
axios.post("http://localhost:8080/reports/proposal-summary/",
payload,
{
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
}
})
.then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf'); //or any other extension
document.body.appendChild(link);
link.click();
})
.catch((error) => console.log(error));
@NayabSiddiqui
Copy link
Author

Answer to the Stackoverflow question Download binary file with Axios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment