What could be causing the issue with 'formData' not functioning in 'axios 0.26.1' on the react native Android emulator?

I recently encountered an issue with the expo-image-picker and axios 0.26.1. It seems that the use of formData in axios version 0.26.1 is not functioning properly, as data is not being sent to the api

https://i.sstatic.net/Bvaou.png

As a workaround, I decided to downgrade axios to version 0.24.0. However, this solution resulted in an error when trying to send formData on an Android emulator.

Error: Network Error

This is how the formData looks like:

https://i.sstatic.net/Knbwb.png

export const sendRequest = (url, response, method, formData) => {
  return axios({
    url,
    method,
    data: method !== "get" ? formData : null,
    headers: {
      Authorization: `Bearer ${response.data.access}`,
      transformRequest: (data, headers) => {
        return formData;
      },
    },
  });

const formData = new FormData();
const imageUri = image.value.uri;
const newImageUri = "file:///" + imageUri.split("file:/").join("");

formData.append("photo", {
  uri: newImageUri,
  type: mime.getType(newImageUri),
  name: newImageUri.split("/").pop(),
});

data.append("title", title);

If anyone has experience with this issue, your help would be greatly appreciated!

Answer №1

An issue was reported on Axios GitHub problems, and it seems to have been happening since version 0.25.0. To resolve the problem, I had to revert back to version 0.24.0. Fortunately, the problem has now been resolved with the following solutions.

Solution 1:

const formData = new FormData();
const response = await axios.post('/URL', formData, {
   headers: {
   ...formData.getHeaders()
   }
});

Solution 2:

axios.post(url, formData, {
  headers: { 'Content-Type': 'multipart/form-data' },
  transformRequest: formData => formData,
})

You can view the resolved issue here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The PUT request is experiencing a timeout issue

After struggling with updating a file in my Mongo DB through a form using a PUT request and Mongoose findByIdAndUpdate, I managed to make it work. The only issue now is that the PUT request seems to be stuck in an infinite loop, leading to a timeout error. ...

Steps for transferring a file from a local directory to a minIO bucket

Hey there, I have a bucket on my Minio server called 'geoxing' and within that bucket, there is a directory named img/site. I'm trying to upload an image directly into the site directory using Node.js. However, when I run the code snippet be ...

Modify HTML using JavaScript with an input field

On a PHP-HTML page, I am looking to dynamically change the position of an image with PHP. I know how to do this, but I also want users to have a preview before submitting the values. <form class="ysize" action="phpchangesize.php" method="get" name="zs" ...

When an object is not empty, the function Object.getOwnPropertyNames will still return an empty array

In my code, I am filling $scope.master with data from a csv file. When I populate $scope.master within the function, all the data is present. This can be observed in the log output displayed below. However, outside of the function, although $scope.master ...

Using Vue js to scrollIntoView will only affect the specific element's div

I am currently working on a Vue page that consists of two divs. The Select-Div contains a list of elements, and when I click on an element from the list, the Scroll-Div is supposed to scroll to the corresponding ID element inside it. Ideally, the scrolling ...

Is there a way to remove the messages I've selected using checkboxes in jQuery?

I'm currently developing a messaging system that heavily utilizes AJAX. One of the features I'm working on is implementing bulk actions with checkboxes. The checkboxes have been added successfully, but I'm facing an issue in figuring out how ...

Add fresh material to the bottom of the page using Javascript

Hey there, I'm having a bit of trouble with my page where users can post their status. I want the new posts to appear at the bottom after the older posts when the user presses the button. Currently, Ajax is placing all new posts at the top of the old ...

Obtaining the complete JSON array in string format

I am currently using Fine Uploader to pass parameters in this way callbacks: { onSubmit: function(id, fileName) { this.setParams({ a: 'adm', b: '126', c: { fileID: id, ...

Electron experiences a crash while attempting to execute an HTTPS request within an addeventlistener callback function

In the process of creating a simple Electron application that facilitates user login into a system, I encounter an issue. The app collects the username and password entered by the user through form text inputs. Upon clicking the "login" button, the program ...

Issue with running "ng serve" command on command prompt

Upon running the command "ng serve" in cmd for Angular2, I encountered the following errors: "unknown browser query basedir=$(dirname $(echo $0 | sed -e s,\,/,g))" After multiple attempts to resolve the issue, I found the error message: Browsersl ...

Dealing with the 'routes not found' error in a Node.js Express application (troubleshooting)

Attempting to address potential missing router errors in my app.js or router file has been a challenge for me. (various solutions found on Stack Overflow have not provided the correct resolution) The current state of my app.js or router files is functiona ...

Angular Firebase Count of Items in List

My current dilemma revolves around obtaining a count of items within a firebase list. Firebase details: foods randompushid title: apple, comboQuery: apple23523526(userid) Angular Code snippet: this.foods= this.firebaseDB.list(& ...

What is the best way to retrieve the height of a <DIV> element without factoring in the presence of a horizontal scrollbar with

I have created a unique jQuery plugin that involves utilizing two nested <DIV> elements. The outer div is set with a fixed width and overflow: scroll, while the inner div (which is wider) houses the content for scrolling purposes. Everything is fun ...

What is the purpose of using multiple css classes together?

In my project, I have implemented CSS modules which allow me to use multiple classes. The variable open is a boolean that determines whether or not the Paper should shift. <Paper className={`${classes.paper} ${open && classes.paperShift}`}> Questio ...

How can we use JavaScript to convert base 10 colors or hex colors to ANSI colors?

I am searching for a method to convert base 10 colors or hex colors into ANSI colors that are compatible with the terminal. For instance, \u001b[31m represents red in ANSI colors. Any suggestions on how this can be achieved using JavaScript code or a ...

Executing a setTimeout function within an Ajax call in a loop

I am attempting to execute a function after a delay of 1 second, but the function is triggered within an ajax call inside a foreach loop. Code: let index = 1; portals.forEach(portal => { setTimeout(() => { $.ajax({ type: "PO ...

Empty body detected in Jquery AJAX request with Django REST running in a Docker environment

Using JavaScript to load a template called index.html from the /static directory. The JavaScript code in app.js: var obj = new Object(); obj.startTime = "123"; obj.endTime = "456"; console.log("fetchNext "+JSON.stringify(obj)); v ...

When attempting to call an XML node with JavaScript in an HTML file within Dreamweaver, the functionality works as expected. However, upon testing the same code on a server

As a beginner in Javascript, I am attempting to use document.write to display some XML data in an HTML format. Below is the code that I have been working on: <script> function loadXMLDoc() { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest( ...

Issue with JavaScript variables (I presume) - there seems to be a conflict when one variable is used alongside another

I am attempting to conduct a test that requires displaying the number of attempts (each time the button is pressed) alongside the percentage of successful attempts, updating each time the button is clicked. However, I've encountered an issue where onl ...

Converting important information from elements in an Array into a string separated by commas

Which lodash method or function is best suited for extracting the ids from the array below and creating a comma-separated string out of them? var myArray = [ { tag: 'wunwun', id: 132 }, { tag: 'davos&apos ...