React-native-fetch-blob: Android: expected dynamic type `string', but had type `int64'

Created on 11 Oct 2016  路  3Comments  路  Source: wkh237/react-native-fetch-blob

Hi and thanks for this amazing package!

I have no trouble sending a file from storage but when I try to send a base64 image I have a mysterious error. It works like a charm on iOS but not on Android. Here is the function I use to send the base64 data:

static async sendBase64File(method, path, base64Data, fieldName, additionalData) {
    let token = await Storage.getUserProperty("token");

    let headers = {
        "Authorization": `Token ${token}`,
        "Content-Type": "multipart/form-data",
        "Cache-Control": "no-store",
    };

    let formData = [{
        name: fieldName,
        filename: fieldName + ".png",
        data: base64Data.replace("data:image/png;base64,", "")
    }];

    for (let key in additionalData) {
        formData.push({name: key, data: additionalData[key]});
    }

    this.onLoadChange(true);
    return RNFetchBlob.fetch(method, "https://[...]", headers, formData)
        .then((response) => {
            // Goes here but no data was sent
        }, (error) => {
            // Not called
        });
}

As a result, the call to the server is indeed made, without any data passed by. Again, this is working 100% fine on iOS. I receive two warnings with the call :

RNFetchBlob failed to create request multipart body :TypeError: expected dynamic type `string', but had type `int64'

and

Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference

So this looks like there is a bug on the Java side.
If anyone could look through this it would be very helpful!

react-native : 0.34
react-native-fetch-blob : 0.9.6

Thanks,
Arthur

needs feedback trouble shooting

Most helpful comment

Hi @wkh237 it now works, after changing the code you mentioned to

for (let key in additionalData) {
       formData.push({name: key, data: String(additionalData[key])});
}

So I guess the native code is waiting for a string, while it should be waiting for any type of JSON object...

Thanks!

All 3 comments

Hi @aouaki , thanks for reporting this issue. According to the error message I assume one of the form field is a number instead of string, could you please check if there's any number in these lines ?

for (let key in additionalData) {
       // if the `data` is a number it would likely cause this error
       formData.push({name: key, data: additionalData[key]});
}

Please feel free leave any comment, thank you 馃槃

Hi @wkh237 it now works, after changing the code you mentioned to

for (let key in additionalData) {
       formData.push({name: key, data: String(additionalData[key])});
}

So I guess the native code is waiting for a string, while it should be waiting for any type of JSON object...

Thanks!

I just wanted to add that also the headers keys must all be of type string. Hopefully this will save time to people in the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jackdoe picture jackdoe  路  4Comments

atasmohammadi picture atasmohammadi  路  3Comments

yaronlevi picture yaronlevi  路  4Comments

jiangbophd picture jiangbophd  路  4Comments

gonglong picture gonglong  路  3Comments