How to post multipart/formdata using fetch in react-native?

jehee choi

reference Image: Postman form

i want to post Form Data like that.

what should i prepare for sending image file data?

i have Uri, type, filename, size.

then will use fetch for it.

Content-type in header is 'multipart/formdata'

thanks for helping

Mikayel Saghyan

You should have an upload function, which should look like this:

upload(url, data) {
  let options = {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    method: 'POST'
  };

  options.body = new FormData();
  for (let key in data) {
    options.body.append(key, data[key]);
  }

  return fetch(requestUrl, options)
      .then(response => {
        return response.json()
          .then(responseJson => {
            //You put some checks here
            return responseJson;
          });
      });
}

And you call it this way, sending the image blob path:

this.upload('http://exampleurl.com/someApiCall', {
  file: {
    uri: image.path,
    type: image.mime,
    name: image.name,
  }
}).then(r => {
  //do something with `r`
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to post a form using fetch in react native?

How to fetch data in React native using axios

how to fetch api(POST) with header in react native app

How can I pass POST parameters in fetch request? - React Native

How to throw Catch in fetch POST request in react native

React native with axios fetch first post delayed

How to change screen after fetch in React Native? [Using Stack Navigator]

How to fetch array and store it in state using hooks React Native

How to post object using fetch with form-data in React?

React - how to Redirect path after completing POST request using fetch

How to send a POST request to Flask API using Fetch in a React app

How to Post data to database using Fetch API in React.js

React_Native Using Post Request with Fetch isn't work(PHP is work normaly)

How can I POST an image to DB via react native with the fetch API?

Using an authorization header with Fetch in React Native

Disable redirect in fetch request using React native

Using curl command for react native to fetch api

Fetch data from API using React Native

Using React Query and Flask to post and fetch

React native fetch token and then get to user details using fetch api

How to upload MultipartFormData with authentication using Alamofire

React Native how to fetch data with API URL

How to fetch complex json into React Native Flatlist?

how to fill picker (android) of react native with fetch

How to change Button on fetch response in react native?

How to output fetch request response in react native

How to set timeout in the fetch function in react native

How to fetch inside a webview in react native?

React Native - fetch method post to Laravel return exception MethodNotAllowedHttpException

TOP Ranking

HotTag

Archive