React Native FormData info is wrapped into "_parts"

Viewed 444

Im a new one in a react native and ran into a problem that FormData behaves itself differently than expected. My code:

let fileToSend = new FormData();
    fileToSend.append('File', {
    name: 'file.name',
    size: 'file.size',
    type: 'file.type',
    uri: 'file://',
});

I expected to get something like this:

fileToSend = {File: [
   name: 'file.name',
   size: 'file.size',
   type: 'file.type',
   uri: 'file://',
]}

But actually Im getting this :

fileToSend = [_parts: [
   0: [
     0: 'File',
     1: {
       name: 'file.name',
       size: 'file.size',
       type: 'file.type',
       uri: 'file://',
        }
]]]

That makes impossible for me to send a file to server. Will be very glad for your help. Thanks.

1 Answers

This is a year and a bit too late, but make sure that you're sending the body as is and it's not being JSON.stringify()'d.

This is what was happening to me so now in our request handler we check to see if the header Content-Type' === 'multipart/form-data'. If it is, we don't stringify the body first.

Related