question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty response when responseType is `blob` or `arraybuffer`.

See original GitHub issue

Summary

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:closed
  • Created 6 years ago
  • Reactions:20
  • Comments:21

github_iconTop GitHub Comments

40reactions
Axnyffcommented, Mar 11, 2018

Here is my full working code:

const FileSaver = require('file-saver');
const axios = require('axios');
axios.get('/file.pdf', {
      responseType: 'arraybuffer',
      headers: {
        'Accept': 'application/pdf'
      }
}).then(response => {
    console.log(response);
    // response.data is an empty object
    const blob = new Blob([response.data], {
      type: 'application/pdf',
    });
    FileSaver.saveAs(blob, Math.random());
});

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: screenshot screenshot bis

12reactions
renetanikolovacommented, Jan 21, 2019

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: image

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.”?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found