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

100
Vistas
axios.post with formData always return empty

Problems

axios.post with formData always return empty object {}

code of react app

const UserSignup = () => {
  
  const signupUser = async (event) => {
    try {
      event.preventDefault();
      const formData = new FormData(document.getElementById('signupForm'));
      const res = await axios.post(
        `${process.env.REACT_APP_BACKEND}/users/signup`,
        formData
      );

      console.log(res.data);
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <Container>
      <h1>signup form</h1>
      <form onSubmit={signupUser} id="signupForm">
        email : <HTextInput name="email" />
        password :
        <HTextInput name="password" type="password" />
        passwordCheck :
        <HTextInput name="passwordCheck" type="password" />
        <Button type="submit">signup</Button>
      </form>
    </Container>
  );
};

I checked..

  • is formData key-values stored?

yes. I checked key-values by adding some codes like

...
const formData = new FormData(document.getElementById('signupForm'));

      // FormData key
      for (let key of formData.keys()) {
        console.log(key);
      }

      // FormData value
      for (let value of formData.values()) {
        console.log(value);
      }

      const res = await axios.post(
        `${process.env.REACT_APP_BACKEND}/users/signup`,
        formData
      );
...

key-values printed in browser console

  • is backend(Nest.js) working well?

yes. I checked with postman and it works perfectly

postman screenshot

backend response(console.log(body))

waiting for some help thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try adding {headers: { "Content-Type": "multipart/form-data" }} as a third argument to your axios.post() call.

about 4 years ago · Juan Pablo Isaza Denunciar

0

1. Test with postman


There's lot of body types like form-data, x-www-form-urlencoded..

I found that

  • post data with form-data return empty {}

but

  • post data with x-www-form-urlencoded return what i expected like { email : "aa@google.com "}

problem was the way receiving formdata in backend(Nest.js)

2. use UseInterceptors


I add code in nest.js like

...

@Post()
  @UseInterceptors(FileInterceptor('file')) // <-- this
createUser(@Body body: any) {
...

it work well i expected whether content-type is default(x-www-form-urlendcoed) or multipart/form-data in React application

I can't exactly explain why this happened and work.

Can someone explain why?

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