最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios error Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream - Stack Overflow

programmeradmin3浏览0评论

I'm getting this error when trying to do a POST request using axios:

Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream at createError

Here's my request:

async function fetchAndHandleErrors() {
  const url = `/claim/${claimId}`;
  const headers = {
    Accept: 'application/json',
    Authorization: `Bearer ${token}`,
  };

  const body = new FormData();
  body.append('damage_description', damageDescription);
  body.append('damaged_phone', {
    uri: imageUri,
    type: 'image/jpeg', // or photo.type
    name: imageUri,
  });

  const result = await axios({
    'post',
    url: `${baseUrl}${url}`,
    data: body,
    headers,
  });
  return result.data;
}

I tried removing result.data and still get the same error. Why is that?

I'm getting this error when trying to do a POST request using axios:

Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream at createError

Here's my request:

async function fetchAndHandleErrors() {
  const url = `/claim/${claimId}`;
  const headers = {
    Accept: 'application/json',
    Authorization: `Bearer ${token}`,
  };

  const body = new FormData();
  body.append('damage_description', damageDescription);
  body.append('damaged_phone', {
    uri: imageUri,
    type: 'image/jpeg', // or photo.type
    name: imageUri,
  });

  const result = await axios({
    'post',
    url: `${baseUrl}${url}`,
    data: body,
    headers,
  });
  return result.data;
}

I tried removing result.data and still get the same error. Why is that?

Share Improve this question asked Jun 28, 2019 at 8:44 Chan Jing HongChan Jing Hong 2,4614 gold badges25 silver badges43 bronze badges 2
  • Any solution for this ? – Pravin Bhapkar Commented Sep 17, 2020 at 12:44
  • I got this error when using axios to post file in nodeJs. The solution for me was to use Node 18 + fetch built-in function – Phạm Huy Phát Commented Oct 31, 2022 at 8:02
Add a ment  | 

3 Answers 3

Reset to default 3

If you eventually still need a solution for this, I managed to get rid of this error by using the formData.pipe() method. For your case, it could look like this :

import axios from 'axios'
import concat from 'concat-stream'
import fs from 'fs'
import FormData from 'form-data'

async function fetchAndHandleErrors() {
  const file = fs.createReadStream(imageUri)

  let body = new FormData();
  body.append('damage_description', damageDescription);
  body.append('damaged_phone', file);

  body.pipe(concat(data => {    
    const url = `/claim/${claimId}`;
    const headers = {
      'Authorization': `Bearer ${token}`,
      ...body.getHeaders()
    };

    const result = await axios({
      'post',
      url: `${baseUrl}${url}`,
      data: body,
      headers,
    });

    return result.data;
  }))
}

Please let me know if you still encounters your issue, I'll be glad to help !

If you don't specify the FormData, it fall to the react interface which is pletely wrong

=> should use 'form-data' from node not react

const FormData = require('form-data');
let formData = new FormData();

I got this error when I am unnecessarily adding a body to DELETE request.

In my case the object I was using as request body value was null which causes this error.

Since the name of the property for data that I'm attaching to the request is data, the error seems kind of obvious but here I am.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论