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
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.
Most helpful comment
Hi @wkh237 it now works, after changing the code you mentioned to
So I guess the native code is waiting for a string, while it should be waiting for any type of JSON object...
Thanks!