React-native-fetch-blob: RNFetchBlob.fetchBlobForm failed to create request body

Created on 19 Jul 2017  路  15Comments  路  Source: wkh237/react-native-fetch-blob

React-Native: 0.46.3
RNFB: 0.10.6

I am trying to upload a selected image from the camera roll using react-native-image-picker. The following works fine on Android but on ios I get warning: Error: RNFetchBlob.fetchBlobForm failed to create request body. Any idea what I'm doing wrong?

    ImagePicker.showImagePicker(options, (response) => {
      if (response.didCancel) {
        console.warn('User cancelled image picker');
      }
      else if (response.error) {
        console.warn('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.warn('User tapped custom button: ', response.customButton);
      }
      else {

        RNFetchBlob.fetch('POST', 'http://12.34.56.78:3000/upload', 
          {'Content-Type' : 'multipart/form-data'}, 
          [
            { name : 'from_device', filename : 'from_device.png', type:'image/png', data: RNFetchBlob.wrap(response.uri)},
          ]
        ).then((resp) => {
          console.warn(resp);
        }).catch((err) => {
          console.warn(err);
        })
ios needs feedback task

Most helpful comment

Because IOS file system does not recognize the file:// URI in the native, so you will need to strip that manually, this is what React Native used to behave in 0.2x. Well, I have to say this is confusing, will fix it once I have time.

All 15 comments

@jstoebel , I'd like to know how is the image's URI look like, perhaps it's caused by #287 .

@wkh237 I took a look at response.uri in the console and here's what I got:

console.warn(response.uri);
// file:///Users/user_name/Library/Developer/CoreSimulator/Devices/C360DD7C-D60鈥30-D5757191483B/Documents/images/E858BC4A-2B68-4FFC-9103-8A9081EDBA08.jpg

console.warn(RNFetchBlob.wrap(response.uri));
// RNFetchBlob-file://file:///Users/stoebelj/Library/Developer/CoreSimulator/D鈥30-D5757191483B/Documents/images/E858BC4A-2B68-4FFC-9103-8A9081EDBA08.jpg

Is the problem something to do with the repeated file:// in the wrapped uri? Sorry if there is something I'm missing.

@jstoebel , perhaps that's exactly what we're going to do in #387, please try to stripe file:// on IOS.

@wkh237 Were you able to track down what is the root cause of the issue? I would like to help out by making a PR to fix this issue.

Because IOS file system does not recognize the file:// URI in the native, so you will need to strip that manually, this is what React Native used to behave in 0.2x. Well, I have to say this is confusing, will fix it once I have time.

I'm having the same issue and removing file:// from the URL does nothing and I'm still getting the same error

let cleanUri =  string_.ltrim(newPhoto.uri, "file:///");
console.log(cleanUri) 
RNFetchBlob.fetch('POST', 'https://api.claudia.constructioncloud.io/teams/' +selectedTeam.id+ '/photos', {
                    Authorization :  `JWT ${accessToken}`,
                    'Content-Type' : undefined, //'multipart/form-data',
                }, [
                    // part file from storage
                    { name : 'file', filename : 'avatar-foo.png', type:'image/jpg', data: RNFetchBlob.wrap(cleanUri)},
                ]).then((resp) => {
                    console.log(resp);
                }).catch((err) => {
                    console.log(err);
                })

I was able to debug this I had the following setting for options under image picker

// Options for photos and videos
const options = {
    title: 'Select',
    noData:true,
    mediaType:'mixed',
    storageOptions: {
        skipBackup: true,
        path: 'construction cloud'
    }
};

path: 'construction cloud'
was casuing my issue, I just changed it to path: 'construction-cloud'

I have this same issue

@Nealyang What issue? I see two issues above and both seem to have been addressed. Did you try what helped the others?

@AlmogRnD you replaced file:/// not file:// (3 slashes)
replacing 2 slashes I managed to read the file on iOS

FYI: for me PATCH works perfect but not POST. So I think bug is inside the package

striping the file:// fixed my issues, but I think this should be patched on library level, any news on this?

@ospfranco

any news on this?

Read the README? :-)

FYI: for me _PATCH_ works perfect but not _POST_. So I think bug is inside the package

That works for me, Thanks.

i solved with string.replace('file://', '')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mykelaballe picture mykelaballe  路  4Comments

atasmohammadi picture atasmohammadi  路  3Comments

Draccan picture Draccan  路  3Comments

sivakumar-cf picture sivakumar-cf  路  4Comments

amirfefer picture amirfefer  路  4Comments