Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Unable to send multipart form data to backend using axios from ReactJS

I am trying to debug this ReactJS code where no matter what I try the backend doesn't receive any data in the body and the file is undefined. I checked by printing all the data(user and element.files[0]) till the line just before the axios request and all seems to be working fine.

Just when the request reaches backend, data doesn't show.

Partial js code:

    async function updateAvatar(element) {
        let data = new FormData();
        // `user` is an object that exists as a state 
        data.append('user', user);
        data.append('file', element.files[0])
        const response = await axios.post(`http://localhost:3001/user/upload_avatar`, data, {
            headers: { "Content-Type": "multipart/form-data" }
        });
    }

Partial HTML code:

<label htmlFor='update-avatar'><span className='btn btn-outline-primary'>Update</span></label>
<input id='update-avatar' type={'file'} className='custom-file-upload' onChange={e => updateAvatar(e.target)} />

The code was working till yesterday and is not sending data properly since today.

Does anyone see any issues with the code?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The solution was really really simple. I had difference in variable name(user) in the frontend FormData() and the backend multer code(user_name). This was causing the issue. By changing the variable name in multer to user, the request works fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!