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

RNFetchBlob.fetchBlobForm failed to create request body

Open hstoebel opened this issue 8 years ago • 15 comments

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);
        })

hstoebel avatar Jul 19 '17 10:07 hstoebel

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

wkh237 avatar Jul 25 '17 02:07 wkh237

@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…C30-D5757191483B/Documents/images/E858BC4A-2B68-4FFC-9103-8A9081EDBA08.jpg

console.warn(RNFetchBlob.wrap(response.uri));
// RNFetchBlob-file://file:///Users/stoebelj/Library/Developer/CoreSimulator/D…C30-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.

hstoebel avatar Jul 25 '17 14:07 hstoebel

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

wkh237 avatar Jul 25 '17 14:07 wkh237

@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.

antondomratchev avatar Jul 25 '17 14:07 antondomratchev

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.

wkh237 avatar Jul 25 '17 14:07 wkh237

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);
                })

isAlmogK avatar Aug 07 '17 02:08 isAlmogK

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'

isAlmogK avatar Aug 07 '17 22:08 isAlmogK

I have this same issue

Nealyang avatar Aug 23 '17 07:08 Nealyang

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

lll000111 avatar Sep 06 '17 02:09 lll000111

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

fabriziomoscon avatar Sep 27 '17 15:09 fabriziomoscon

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

DmytroBatyuk avatar Jan 26 '18 13:01 DmytroBatyuk

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

ospfranco avatar Feb 20 '18 08:02 ospfranco

@ospfranco

any news on this?

Read the README? :-)

lll000111 avatar Feb 20 '18 08:02 lll000111

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

That works for me, Thanks.

delvinmc20 avatar Nov 20 '18 00:11 delvinmc20

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

vaqif14 avatar Dec 13 '18 13:12 vaqif14