Home > front end >  Converting response to arraybuffer
Converting response to arraybuffer

Time:03-10

I am trying to convert a response to arraybuffer with axios. I keep getting an error message: "TypeError: response.arrayBuffer is not a function". Below is the code I wrote and suggestion on what I am doing wrong or a solution to this error.

 axios
      .post("http://127.0.0.1:8000/api/file-api", formData, {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          'Access-Control-Allow-Origin':'http://localhost:8080',
          'Access-Control-Allow-Credentials': 'true', 
          'Authorization': `bearer ${token}`
          
        },
      })
      .then((response) => response.arrayBuffer())
      .then((res) => {
        const arrayBuffer = res.arrayBuffer();
        const objUrl = window.URL.createObjectURL(new Blob([arrayBuffer]));
        return objUrl;

        

        
      })
      .catch((err) => {
        console.log(err);
      });

CodePudding user response:

After reading this post [Axios vs. fetch(): Which is best for making HTTP requests?][1],

[1]: https://blog.logrocket.com/axios-vs-fetch-best-http-requests/#:~:text=Axios automatically transforms the data,info on what the response. and then the comment by @Bravo, I notice using axios may not be able to provide an arraybuffer as a responseType. In the case of arraybuffer, it is best to use fetch.

  • Related