2

I want to send multipart data to the server with XMLHttpRequest using React Native.

My code is as follows:

let body = new FormData();
const photo = {
    uri:  picture.picture.path, //"https://autralis.blob.core.windows.net/devthumbnails/1024x768/79/55/99/79559918324723216216211601856493692575062394716602088366146664699383830311185.jpg"
    type: 'image/jpeg',
    name: uuid + '.jpg', //66f8580e-4d99-46cb-b8db-e5f13640367c.jpg
};
body.append('photoGUID', uuid); //66f8580e-4d99-46cb-b8db-e5f13640367c
body.append('item', picture.item); //front-left
body.append('photo', photo);

let xhr = new XMLHttpRequest();
xhr.open('POST', Common.mainUrl +'/manager/api/v1/sync/request/picture/');

xhr.onload = function (e) {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            console.log("Success");
        }
    }
};
xhr.onerror = function (e) {
    console.log(JSON.stringify(e));
};
xhr.send(body);

On IOS it works fine, as expected.

But on Android it doesn't work. If I send only photo; thus without item and photoGUID it works fine, but if I want to send photo and the data than it won't work.

It comes in xhr.onerror and the error message is as follows:

...
_response": "Could not retrieve file for uri https://autralis.blob.core.windows.net/devthumbnails/1024x768/79/55/99/79559918324723216216211601856493692575062394716602088366146664699383830311185.jpg"
...

Any idea?

Boky
  • 11,554
  • 28
  • 93
  • 163
  • I have no experience with `XMLHttpRequest`, but I have used multipart data with [fetch](https://facebook.github.io/react-native/docs/network.html#using-fetch) and [apisauce](https://github.com/skellock/apisauce). You can read more [here](https://stackoverflow.com/a/36649457/5315786) if you want to use `fetch` or any service (not `XMLHttpRequest`) – Pir Shukarullah Shah Sep 25 '17 at 11:10
  • @ShukarullahShah Thanks. The problem is that this code works also fine if I upload only the photo, as it is in the your link. But if I want to add extra data, than it fails. But only on android, on IOS it works perfect. – Boky Sep 25 '17 at 11:32
  • I have really no idea why it fails only on Android. Let me know when you success. Best of luck. – Pir Shukarullah Shah Sep 25 '17 at 12:22
  • @ShukarullahShah Thanks. – Boky Sep 25 '17 at 12:34

0 Answers0