request formData to API, gets “Network Error” in axios while uploading image

隐身守侯 提交于 2020-12-31 05:04:45

问题


I am making a POST request to server to upload an image and sending formdata using axios in react-native. i am getting "Network Error". i also try fetch but nothing work.using react native image picker libeary for select image.in postman api working fine

        formData.append('title', Title);
        formData.append('class_id', selectClass._id)
        formData.append('subject_id', checkSelected)
        formData.append('teacher_id', userId)
        formData.append('description', lecture);
        formData.append('type', 'image');

       var arr=[];
       arr.push(imageSource)
       arr.map((file,index)=>{
       formData.append('file',{
       uri:file.path,
       type:file.type,
       name:file.name
       })
       })


       axios({
       method: 'post',
       url: URL + 'admin/assignment/create',
       data: data,
       headers: {
       "content-type": "multipart/form-data",
       'x-auth-token': token,
        },
       })
     .then(function (response) {
    //handle success
    console.log('axios assigment post',response);
      })
   .catch(function (response) {
     //handle error
      console.log('axios assigment post',response);
    });

回答1:


Project keeps flipper java file under app > source > debug in react native > 0.62. There is an issue with Flipper Network that causes the problem in your case. If you remove the debug folder, you will not be able to debug Android with Flipper, so the best solution is upgrading Flipper version in android > gradle.properties to 0.46.0 that fixes the problem.

You can change it with this line FLIPPER_VERSION=0.46.0

react-nativeandroid




回答2:


I faced the same issue. The following steps worked for me.

  1. update FLIPPER_VERSION=0.52.0 latest
  2. for formData code as below:
let formData = new FormData();
let file = {
          uri: brand.uri, 
          type: 'multipart/form-data', 
          name: brand.uri
};
formdata.append('logo', file);

The type must be 'multipart/form-data' as the post header.




回答3:


REACT NATIVE SOLUTION

If you are using Axios or Fetch in React Native and you got Network Error when uploading the file or data.

Try to commenting below line from the /android/app/src/main/java/com/{your_project}/MainApplication.java

its located around the 40-50 line

initializeFlipper(this, getReactNativeHost().getReactInstanceManager())

https://github.com/facebook/react-native/issues/28551




回答4:


"react-native": "0.62.1", "react": "16.11.0", "axios": "^0.19.2",

weird solution i have to delete debug folder in android ->app->source->debug

and restart the app again its solve my problem. i think it's cache problem.




回答5:


I had this problem and solve it via commenting the 43 line in android/src/debug/.../.../ReactNativeFlipper.java

// builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));

could you test it?




回答6:


in this file android/app/src/debug/java/com/**/ReactNativeFlipper.java

43 // builder.addNetworkInterceptor(new strong textFlipperOkhttpInterceptor(networkFlipperPlugin));



来源:https://stackoverflow.com/questions/61175557/request-formdata-to-api-gets-network-error-in-axios-while-uploading-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!