Axios image upload using post form data always returns network error

Viewed 1908

Having problem uploading an image on android running react native using axios. I have researched a lot about this and have tried a lot of things for the past 3days but no solution yet. This seem to be a very common problem on android. Any help is appreciated.

let url = "myURL will be here"
let uploadBody = new FormData()
uploadBody.append('photos',{uri:this.state.coverImage[0].uri,type: 'image/jpeg',
    name: 'coverimage'})
axios.post(url, 
{
method: "POST",
body: uploadBody,
headers : {
"Content-Type":"application/x-www-form-urlencoded"
}
}).then((resp) => {
console.log(resp);
}).catch(err => {
console.log(err);
});

The api works fine on postman. I am able to upload images. And i get a response { "success": true }

Getting this error on android

Error: Network Error
at createError (createError.js:15)
at XMLHttpRequest.handleError (xhr.js:88)
at XMLHttpRequest.dispatchEvent (event-target-shim.js:818)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:574)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:388)
at XMLHttpRequest.js:501
at RCTDeviceEventEmitter.emit (EventEmitter.js:189)
at MessageQueue.__callFunction (MessageQueue.js:395)
at MessageQueue.js:106
at MessageQueue.__guard (MessageQueue.js:343)
1 Answers

your passing body is true check your internet network.

Related