Why does 'formData' not work in 'axios 0.26.1' in react native Android emulator?

Viewed 531

I have use expo-image-picker and axios 0.26.1. formData in axios version 0.26.1 isn't work. when using formData, data is not sent to the api

enter image description here

downgrade axios to version 0.24.0 but I get this error when sending formData in Android emulator.

Error: Network Error

formData:

enter image description here

export const sendRequest = (url, response, method, formData) => {
  return axios({
    url,
    method,
    data: method !== "get" ? formData : null,
    headers: {
      Authorization: `Bearer ${response.data.access}`,
      transformRequest: (data, headers) => {
        return formData;
      },
    },
  });

const formData = new FormData();
const imageUri = image.value.uri;
const newImageUri = "file:///" + imageUri.split("file:/").join("");

formData.append("photo", {
  uri: newImageUri,
  type: mime.getType(newImageUri),
  name: newImageUri.split("/").pop(),
});

data.append("title", title);

can you help me please?

1 Answers

This bug has been posted on Axios GitHub issues and apparently it is present since 0.25.0. I had to roll back to version 0.24.0 to make it work. However, this issue has been closed with the following solutions.

Method 1:

const formData = new FormData();
const response = await axios.post('/URL', formData, {
   headers: {
   ...formData.getHeaders()
   }
});

Method 2:

axios.post(url, formData, {
  headers: { 'Content-Type': 'multipart/form-data' },
  transformRequest: formData => formData,
})

You can check the issue here

Related