Search code examples
node.jsfetchreact-native-cli

Upload image to a nodeJs server using react-native


I want to upload an image from react-native (cli without expo) to a node server, i tried using axios with formData filed is not recognised by multer, then i tried with fetch that worked image was stored in server but i get promise rejection on front:

const postDocument = (doc) => {
//i used react-native-document-picker to pick document(doc) i logged it and it works 
  const url =`${api}/sendImage`;
  const formData = new FormData();
  const id = uuid.v4()

  formData.append('id',id)
  formData.append('image', doc);
 const options = {
      method: 'POST',
      body: formData,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'multipart/form-data',
      },
  };
  fetch(url, options).then((res)=>{
//getting promise rejection even without code but image is stored in backend
  }).catch(error=>{})
}

getting this error : [TypeError: Network request failed]


Solution

  • using rn-fetch-blob (react-native-cli not expo-cli) instead of fetch or axios solved this for me