0

I have this code to upload mp3 audio to a server but it gives me [TypeError: Network request failed] Help please

export async function uploadFile(fileUri) {
  const url = //my url;
  var token;
  await AsyncStorage.getItem('access_token').then((res) => {
    token = res;
  });
  let data = new FormData();

  data.append('file', {
    uri: fileUri,
    name: 'test.mp3',
    type: 'audio/mpeg',
  });
  return fetch(url, {
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + token,
      'Content-Type': 'multipart/form-data',
    },
    body: data,
  })
    .then((response) => response.json())
    .catch((error) => console.error(error));
}

The same exact code works fine for an image uri if I change

data.append('file', {
    uri: fileUri,
    name: 'test.mp3',
    type: 'audio/mpeg',
  });

to

 data.append('file', {
    uri: fileUri,
    name: 'test.jpg',
    type: 'image/jpeg',
  });

I'm sure the audio's uri is correct because I use the exact same way to get the uri of the image which works fine.

I tried audio/mpeg and audio/mp3 for the type but neither of them works.

0 Answers0