Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda