Open myomyintaung1411 opened 4 years ago
any solution?????
URGENT !!!! please reply if possible
You should change like this,
{
// headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data,octet-stream',
},
So with fetch() in javascript you need headers: {} but when switching over to RNFetchBlob you should put the headers directly in the {}
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);
});
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() }) })
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); });
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....
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); });