react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Unable to upload image file on first try getting TypeError: Network Request Failed

Open ws8149 opened this issue 3 years ago • 18 comments

Description

This issue only occurs on Android devices after updating React Native from v0.63.4 to v0.65.1. For some strange reason, the first attempt at uploading a image file will immediately fail with the error TypeError: Network Request Failed.

If it does fail on the first try, the second or third will succeed. Does anyone know why does this happen or how I can investigate this further? Sometimes this issue does not even happen at all.

Version

0.65.1

Output of npx react-native info

npm ERR! code E404 npm ERR! 404 Not Found - GET https://registry.npmjs.org/react-native-info - Not found npm ERR! 404 npm ERR! 404 'react-native-info@latest' is not in the npm registry. npm ERR! 404 You should bug the author to publish it (or use the name yourself!) npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\ws8149\AppData\Roaming\npm-cache_logs\2022-01-25T12_34_29_576Z-debug.log Install for [ 'react-native-info@latest' ] failed with code 1

Steps to reproduce

  1. Use an Android device
  2. Perform an image file upload

Snack, code example, screenshot, or link to a repository

const handleUpload = async (file) => {
    const form = new FormData()

    form.append('file', {
      uri: file.uri, type: file.type, name: file.name
    })

    const token = await getItem(storageKey.token)

     let req_headers = {
       'Content-Type': 'multipart/form-data',
      'Accept': 'application/json',
       Authorization: `Bearer ${token}`,       
     };     

     fetch(`${API_URL}/upload`, {
      method: 'POST',
      body: form,
      headers: req_headers
    })
      .then((response) => response.json())
      .then((response) => {
        console.log('response', response);
      })
      .catch((error) => {
        console.log('error', error);       
      });
     
   };

ws8149 avatar Jan 25 '22 12:01 ws8149

Maybe you can try to close your browser devtool , somtimes it will block the upload request

Bravo-Lin avatar Jan 28 '22 07:01 Bravo-Lin

Thanks but its also occurring on release builds

ws8149 avatar Feb 07 '22 04:02 ws8149

This problem also occurs on a fresh clean react native project.

The dependencies used in the new project were

"axios": "0.19.2",

"react": "17.0.2",

"react-native": "0.67.4",

"react-native-image-picker": "3.4.0"

The code used was:

        const instance = axios.create({
          baseURL: ENV.API_URL,
          timeout: 10000,
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
            'X-AUTH-TOKEN': 'token',
            'Authorization': 'Bearer ${token}'
          },
        })

       const options = {
          quality: 0.6,
          mediaType: 'photo',
          maxWidth: 500,
          maxHeight: 500,
        }

        launchImageLibrary(options, async (file) => {
          
          const form = new FormData()

          const my_file = {
            uri: file.uri, type: file.type, name: file.fileName
          }

          form.append('file', my_file)

          await instance({                
            method: 'post',
            url: '/upload',
            data: form,          
            headers: {
              'Content-Type': 'multipart/form-data'
            },       
          }).then((res) => {
            console.log(res.data)    
            Alert.alert('TEST', 'UPLOAD SUCCESS')            
          }).catch((err) => {
            console.log(err)
            Alert.alert('TEST', 'UPLOAD FAILED')            
          })
        })

The error shown in the logs were 2022-03-17 10:52:00.969 I/ReactNativeJS: 'err: ', { [Error: Network Error] request: { UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4, readyState: 4, status: 0, timeout: 10000, withCredentials: true, upload: {}, _aborted: false, _hasError: true, _method: 'POST', _response: 'Stream Closed',

Does anyone know why the stream closes immediately on the first try?

ws8149 avatar Mar 18 '22 04:03 ws8149

"react-native": "0.65.1", Having the same problem as you ,did you solve it?

zgj0303 avatar Mar 28 '22 03:03 zgj0303

No, I'm still having the same issue unfortunately.

ws8149 avatar Mar 29 '22 03:03 ws8149

I solve this problem by using rn-fetch-blob.

font-ds avatar Jun 05 '22 12:06 font-ds

add 'Content-Type': 'multipart/form-data' in your headers will resolve the issue

ishaqashraf avatar Jan 10 '23 14:01 ishaqashraf

Hello,

I have exactly the same problem with [email protected] and [email protected], have you found a solution ?

Thanks :)

GaylordP avatar Jan 29 '23 10:01 GaylordP

@GaylordP Hey, we're experiencing this same issue. Have you found a way to resolve this yet?

Killyannoor avatar Feb 15 '23 14:02 Killyannoor

Hi, any solutions? I have this same issue on android.

Arek1020 avatar Mar 16 '23 12:03 Arek1020

@Arek1020 In my case, downgrading Axios worked. I downgraded to 0.24.0

Killyannoor avatar Mar 17 '23 08:03 Killyannoor

Same issue ! PLEASE HELP

anikettari avatar Apr 05 '23 06:04 anikettari

This worked for me on EXPO SDK 49: I've installed expo-build-properties using npx expo install expo-build-properties and then added it as a plugin in app.json:

    "plugins": [
      [
        "expo-build-properties",
        {
          "android": {
            "usesCleartextTraffic": true
          }
        }
      ]
    ]

GeorgeFlorian avatar Sep 25 '23 13:09 GeorgeFlorian

This worked for me on EXPO SDK 49: I've installed expo-build-properties using npx expo install expo-build-properties and then added it as a plugin in app.json:

    "plugins": [
      [
        "expo-build-properties",
        {
          "android": {
            "usesCleartextTraffic": true
          }
        }
      ]
    ]

not working for cli

theharshkonda avatar Oct 02 '23 13:10 theharshkonda

any solution so far??

notDemian avatar Oct 27 '23 15:10 notDemian

any solution so far??

This one worked for me: https://github.com/facebook/react-native/issues/32966#issuecomment-1733684799

MetriciRO avatar Oct 29 '23 11:10 MetriciRO

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Apr 27 '24 05:04 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar May 04 '24 05:05 github-actions[bot]