joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.84k stars 784 forks source link

[Error: RNFetchBlob request error: Value for headers cannot be cast from ReadableNativeMap to Stringnull] #518

Open myomyintaung1411 opened 4 years ago

myomyintaung1411 commented 4 years ago

const formData = new FormData(); const file = { uri: response.uri, type: 'multipart/form-data', name: response.fileName, }; formData.append('file', file); RNFetchBlob.fetch('POST', 'http://10.0.2.2:3000/upload', { // header... headers: { Accept: 'application/json', 'Content-Type': 'multipart/form-data,octet-stream', }, body: formData, }) .then(res => { console.log('res', res); }) .catch(err => { console.log('err', err); });

ajinkyadesai-git commented 4 years ago

any solution?????

ajinkyadesai-git commented 4 years ago

URGENT !!!! please reply if possible

timo-kang commented 4 years ago

You should change like this,

 {
// headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data,octet-stream',
},
jeremyfrancis commented 4 years ago

So with fetch() in javascript you need headers: {} but when switching over to RNFetchBlob you should put the headers directly in the {}

ajinkyadesai-git commented 4 years ago

This works in case of 'Content-Type': 'multipart/form-data': (ignore comments)

RNFetchBlob.fetch('POST','http://192.168.2.214:8080/api/files/upload',
                                {
                                    // header...
                                    username: savedUserName,
                                    token: savedtoken,
                                    // 'Content-Type': 'application/octet-stream',
                                    'Content-Type': 'multipart/form-data',
                                    // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
                                    // Or simply wrap the file path with RNFetchBlob.wrap()
                                },

                                // RNFetchBlob.wrap(response.path)
                                [

                                    {
                                        name: 'file',
                                        filename: 'photo.jpg',
                                        type: 'image/png',
                                        data: RNFetchBlob.wrap(response.uri)
                                    },

                                ]
                            ).then(res => {
                                // alert(res);

                                this.setState({ progressVisible: false });
                                alert("file " + response.fileName + "Upload!");
                                console.log("res:", res.text());

                            })
                                .catch(err => {
                                    this.setState({ progressVisible: false });
                                    alert("Server down, please try in some time");
                                    console.log('Upload err!!', err);

                                });
AravindTReddy commented 4 years ago

Any solution for this ??? I have my fetch body like this and i get the error " RNFetchBlob request error: Value for headers cannot be cast from ReadableNativeMap to Stringnull "

.fetch('POST', 'https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/export', { headers:{ 'Accept': 'application/json', 'Content-type': 'application/json' }, body:JSON.stringify({ deviceid: item, fromtime: myEpoch1.toString(), totime: myEpoch2.toString() }) })

Rehanmp commented 2 years ago

So with fetch() in javascript you need headers: {} but when switching over to RNFetchBlob you should put the headers directly in the {}

Also if you want to add body in the request then directly add it there. For example:- await config(options) .fetch('POST', PDFURL, { accept : 'application/pdf' }, JSON.stringify(reqBody) ) .then(res => { console.log("response body>>>",res); }) .catch(function (err) { console.log(err); });

mahendrenkce commented 2 years ago

So with fetch() in javascript you need headers: {} but when switching over to RNFetchBlob you should put the headers directly in the {}

Also if you want to add body in the request then directly add it there. For example:- await config(options) .fetch('POST', PDFURL, { accept : 'application/pdf' }, JSON.stringify(reqBody) ) .then(res => { console.log("response body>>>",res); }) .catch(function (err) { console.log(err); });

This is more helpful. Thank you so much.. you are save my time....