0

In my vuejs 2 application, I am trying to migrate to axios.

the code worked using Request-Promise, but now I get this error:

First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object

--------> file1.ts
///////////////

import axios from 'axios';

const getAgentFile = async (agentName: string): Promise<any> => {
const url = `${BASE_URL}/${agentName}/export`;
const opt = {
  headers: {
    encoding: null,
    'content-type': 'application/json',
    Authorization: `Bearer ${store.getters.authToken()}`,
  },
};
const agent = await request.get(url, opt);
return agent.data;
};

--------> file2.ts
///////////////

async downloadAgentFile(name: string) {
      const response = await getAgentFile(name);
      const buffer = Buffer.from(response);
      const fileURL = window.URL.createObjectURL(new Blob([buffer]));
      const fileLink = document.createElement('a');
      fileLink.href = fileURL;
      fileLink.setAttribute('download', `${name}.json`);
      document.body.appendChild(fileLink);
      fileLink.click();
    },

Any idea please ?

Michal Levý
  • 33,064
  • 4
  • 68
  • 86
Amir Choubani
  • 829
  • 2
  • 14
  • 28

1 Answers1

0

The solution is to specify responseType: 'arraybuffer'.

import axios from 'axios';

const getAgentFile = async (agentName: string): Promise<any> => {
const url = `${BASE_URL}/${agentName}/export`;
const opt = {
  responseType: 'arraybuffer', // this is the missing part
  headers: {
    encoding: null,
    'content-type': 'application/json',
    Authorization: `Bearer ${store.getters.authToken()}`,
  },
};
const agent = await request.get(url, opt);
return agent.data;
};

I hope It helps others :)

Amir Choubani
  • 829
  • 2
  • 14
  • 28