Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use fetch to post a blob in react-native

I'm trying to post a blob. It's definitely a blob. This isn't working in react-native though. I'm getting a red screen that says "PUT must have a request body". Well, I've put the blob in the request body.

createAttachment: function(url, blob) {
  var settings = {
    method: "PUT",
    headers: {
      'Accept': 'image/jpeg',
      'Content-Type': 'image/jpeg',
      'If-Match': '*',
    },
    body: blob
  };
  return fetch(url, settings)
}
like image 753
ssomnoremac Avatar asked Mar 03 '16 17:03

ssomnoremac


People also ask

What is this React Native blob project?

This project was started in the cause of solving issue facebook/react-native#854, React Native's lacks of Blob implementation which results into problems when transferring binary data. It is committed to making file access and transfer easier and more efficient for React Native developers.

What is the fetch API in React Native?

For this purpose, the fetch API provides a generic definition for the Request and Response objects as well as other aspects of network requests, serving as a mechanism to retrieve resources and interact with a REST API or the cloud. As described in the React Native docs: "React Native provides the Fetch API for your networking needs.

What is the difference between fetch and useeffect in react?

This sends the same POST request from React using fetch, but this version uses React hooks from a function component instead of lifecycle methods from a traditional React class component. The useEffect React hook replaces the componentDidMount lifecycle method to make the HTTP POST request when the component loads.

When should I skip the “linking” step in React Native?

Linking can be skipped when the project uses React Native 0.60 or greater, because autolinking will take care of it If CocoaPods is used in the project, make sure to install the pod:


1 Answers

My project had same problem before, according to this issue perhaps, blob data is not supported in react native fetch API currently (Both in send and receive).

So I made a module myself ..

https://github.com/wkh237/react-native-fetch-blob

It works fine in our project, if you don't mind to take a look, it might helps.

like image 83
Xeijp Avatar answered Sep 18 '22 15:09

Xeijp