react-native-blob-courier icon indicating copy to clipboard operation
react-native-blob-courier copied to clipboard

Option to pass body to fetchBlob

Open ezze opened this issue 2 years ago • 10 comments

Is your feature request related to a problem? Please describe.

I am trying to download a big file using our API

const options: BlobFetchInput = {
  method: 'POST',
  filename: 'offline.db',
  headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  android: {
    target: 'data'
  },
  mimeType: 'application/octet-stream',
  url: `${apiUrl}api/v1/client-db/clone`
};

const response = await BlobCourier.fetchBlob(options);

JSON body with request parameters must be part of the request but unfortunatelly I didn't find such option for fetchBlob.

Describe the solution you'd like

I want my request to look like this:

const options: BlobFetchInput = {
  method: 'POST',
  filename: 'offline.db',
  headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ facilityIds })
  android: {
    target: 'data'
  },
  mimeType: 'application/octet-stream',
  url: `${apiUrl}api/v1/client-db/clone`
};

Describe alternatives you've considered

At the moment I use FileSystem.fetch from react-native-file-access but unfortunately it doesn't provide a way to cancel a long request:

const options = {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  path: getDatabasePath(dbName),
  body: JSON.stringify({ facilityIds })
};
const url = `${apiUrl}api/v1/client-db/clone`;
const response = await FileSystem.fetch(url, options, onProgress);

ezze avatar Nov 19 '21 12:11 ezze

Hi @ezze , thank you for your suggestion! It seems like a fair request: I'll put it on the backlog and link this issue to the PR as soon as I find the time to work on it.

edeckers avatar Nov 20 '21 14:11 edeckers

@edeckers Great! Thank you for a quick reply! I really appreciate your efforts on supporting this feature! It would be awesome to have this one.

ezze avatar Nov 22 '21 08:11 ezze

@edeckers Just wanted to ask you whether it's difficult to implement this feature in the lib. I know, you support it in your free time so it's not a request from me.

I am considering options to interrupt uploads/downloads. While option to interrupt uploads only is available in react-native-blob-courier I was thinking to switch to downloadFile from react-native-fs but unfortunately (suprisingly) it can't work with methods other than GET: https://github.com/itinance/react-native-fs/issues/407 :roll_eyes:

ezze avatar Dec 07 '21 21:12 ezze

It's not too complicated in terms of code complexity, but I think it will amount to quite some lines and changes; more than you'd expect anyway. Something along the lines of https://github.com/edeckers/react-native-blob-courier/pull/141/files, and that PR doesn't even include tests yet; typically each PR adds multiple tests for TypeScript, Android and iOS.

So I'd like to add the feature and help you out, but as you can imagine December isn't the best month to find time for that, what with the Holidays and all that stuff going on. January seems feasible for me.

In addition I'd like to mention that I welcome PR's, if you feel like making a start yourself! Should you consider doing so, let's figure out a contract for the interface together before you start working on it, so your hard work won't be for naught.

edeckers avatar Dec 10 '21 11:12 edeckers

@edeckers Thanks for your reply, mate. The project I am working on at the moment is my first React Native application so I'm not sure that it will be easy for me to dig into the issue and implement the feature I need in a short time range. Although, it's interesting for me. I have to spend some time to understand how all these native things are built and work first.

So I'll look for a cheaper way to implement the required feature by trying another libs. But if I have no luck and our business partners are ready to pay for this then I'll back to you and dig into implementing this feature.

Thanks for your responsiveness and helpfulness. Really appreciate it!!!

ezze avatar Dec 15 '21 10:12 ezze

I totally get where you're coming from, hopefully react-native-file-access solves the issue for you. Either way I'm still considering adding this feature in January / early 2022, so make sure to check back every now and then!

edeckers avatar Dec 17 '21 12:12 edeckers

@edeckers I like your library too much so decided to migrate to GET request with query parameters instead of POST with body as a temporary workaround. It was before author of react-native-file-access had impemented the missing feature, so I'm happy with current solution now. :tada:

ezze avatar Dec 17 '21 13:12 ezze

Haha, thanks for the compliment and nice workaround!

edeckers avatar Dec 17 '21 13:12 edeckers

It is a very nice feature. I'm also using react-native-file-access to download files beucase the lack of send body data on post requests.

EduFrazao avatar Nov 08 '22 18:11 EduFrazao

Hi @EduFrazao , thank you for reaching out! I agree that this is a nice and even missing feature that I'd love to add. There have been some unforseen life events (nothing too serious) for me though, which resulted in me not being able to find the right time and mindset to pick this up.

Still planning on adding it, but right now I can't make any actual promises about when I'll get to it.

edeckers avatar Nov 11 '22 17:11 edeckers