Rn-fetch-blob: RNFetchBlob failed to create request multipart body :Value for data cannot be cast from Double to String

Created on 23 Jun 2019  路  3Comments  路  Source: joltup/rn-fetch-blob

Hi
In android I am getting below errors on console

RNFetchBlob failed to create request multipart body :Value for data cannot be cast from Double to String

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

this is how i wrote the code

RNFetchBlob.fetch('POST', location, {
    Authorization: token,
    'Content-Type': 'multipart/form-data',
  },[
        { name: 'home_service_id', data: this.serviceId},
        { name: 'order_type', data: 'hourly'},
        { name: 'timing_constraints', data: form.timingConstraints},
        { name: 'notes', data: form.jobDetails},
        { name: 'order_details_attributes', data: form.jobDetails},
        { name: 'customer_address_id', data: form.address.addressId.toString()},
{ name : 'images[]', filename : photoItem.filename, type:photoItem.mime, data: RNFetchBlob.wrap(photoItem.path)}
      ])
    .uploadProgress((written, total) => {
      //console.log('uploaded', written / total)
    })
    .then((resp) => {
      console.log(resp);
      return {error: null, response: resp};
    })
    .catch((err) => {
      console.log(err);
      //return { error: err };
      return {error: err, response: null};
    })

Thanks for help in advance

Most helpful comment

The data should be a string, cast to string before sending. That worked for me.

{ name: 'home_service_id', data: String(this.serviceId)} // if serviceId is a number

All 3 comments

The data should be a string, cast to string before sending. That worked for me.

{ name: 'home_service_id', data: String(this.serviceId)} // if serviceId is a number

Got the same issue, I'm using react native image picker. I passed data like this
{
name: 'file',
filename: file.filename,
type: file.type,
data: file.data, // tried with file.uri, file.origUrl, RNFetchBlob.wrap(file.uri), RNFetchBlob.wrap(file.origUrl), none of them work
},

Hey @baotoan1905, did you have any other items in your form? I focused on a file item until I figured out it was a field in another item that was causing the error.

const form = [
{ name : 'file', filename : name,
// Change BASE64 encoded data to a file path with prefix RNFetchBlob-file://.
// Or simply wrap the file path with RNFetchBlob.wrap().
type: "audio/aac",
data: "RNFetchBlob-file://"+file},
{name: "id", data: String(id)}, // <== not casting to String() was causing the error.
{name: 'dw', data: "saveAac"}
];

Was this page helpful?
0 / 5 - 0 ratings