Home > Software design >  How to send formdata and image file in react native expo?
How to send formdata and image file in react native expo?

Time:05-30

Hi Everyone I want to send Formdata which includes some strings and an image file using react native expo to node server and mongo db can someone explain how can I do this using react native expo I have tried several methods but I am not successful however I am able to send data using backend but I don't know how to send using React native expo?

CodePudding user response:

one of the libraries you can use to send data from react native is Axios you can install it via :

npm i axios

and here is how you can send data with that :

You can post axios data by using FormData() like:

var bodyFormData = new FormData();

And then add the fields to the form you want to send in case you can use append:

bodyFormData.append('userName', 'Fred');

If you are uploading images, you may want to use .append

bodyFormData.append('image', imageFile); 

And then you can use axios post method (You can amend it accordingly)

axios({
  method: "post",
  url: "myurl",
  data: bodyFormData,
  headers: { "Content-Type": "multipart/form-data" },
})
  .then(function (response) {
    //handle success
    console.log(response);
  })
  .catch(function (response) {
    //handle error
    console.log(response);
  });

related documents you may be interested in: https://axios-http.com/docs/api_intro

  • Related