Empty response when responseType is `blob` or `arraybuffer`.
See original GitHub issueSummary
Axios can’t handle blob
and arraybuffer
responseType. It returns empty oject in response.data
.
For example, downloading of PDF file:
axios.get('/file.pdf', {
responseType: 'arraybuffer',
headers: {
'Accept': 'application/pdf'
}
}).then(response => {
// response.data is an empty object
const blob = new Blob([response.data], {
type: 'application/pdf',
});
FileSaver.saveAs(blob, 'file.pdf');
});
The contend of downloaded file is:
[object Object]
Is it a bug?
Context
- axios version: v0.18.0
- Environment: node v9.5.0, chrome 64, archlinux
Issue Analytics
- State:
- Created 6 years ago
- Reactions:20
- Comments:21
Top Results From Across the Web
how does axios handle blob vs arraybuffer as responseType?
From axios docs: // `responseType` indicates the type of data that the server will respond with // options are: 'arraybuffer', 'document', ...
Read more >XMLHttpRequest.responseType - Web APIs | MDN
An empty responseType string is the same as "text" , the default type. ... The response is a JavaScript ArrayBuffer containing binary data....
Read more >HttpRequest - Angular
responseType : 'arraybuffer' | 'blob' | 'json' | 'text', Read-Only. The expected response type of the server. This is used to parse the...
Read more >XMLHttpRequest.response - Web APIs
An empty responseType string is treated the same as "text" , the default type. arraybuffer: The response is a JavaScript ArrayBuffer containing binary...
Read more >How to handle error message and binary response with Axios ...
But how to, as Axios will convert responses according to your specific response type? The answer is specifying response type as ArrayBuffer rather...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is my full working code:
I added the Math.random part to make sure I was generating a new pdf. Can you provide more info on your server, there might be something here that is throwing axios off.
Here are some more picture of what the headers look like and the content of the file:
How do you catch an error if the request fails? Since we have: responseType: “blob” - the method expects blob object and if the request fails, that’s what I get in the error.response object:
and there is no error message available there. So, how do you handle the error, or you just throw an error with the same message every time, like “Error occurred while generating the pdf file.”?