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

202
Vistas
form-data in postman sending Empty object to node.js

I know this has been asked multiple times, but I have been looking around and still can't find an answer to my problem.

index.js file

const express = require("express");
require("dotenv").config({
  path: ".env",
});
const PORT = process.env.PORT || 5000;
const runDatabase = require("./config/database");
const path = require('path')

const app = express()
const cors = require('cors')

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }));

app.use("/uploads", express.static(path.join(__dirname, "uploads")));

// routers
const userRouter = require("./router/usersRouter");
const categoryRouter = require("./router/categoryRouter");
const productRouter = require("./router/productRouter");

app.use("/api", userRouter);
app.use("/api", categoryRouter);
app.use("/api", productRouter);

app.listen(PORT, () => {
  runDatabase();
  console.log(`🚀The Backend Server is up and running on port ${PORT}🚀`);
});


controller file

exports.createCategory = async (req, res) => {
  try {
    const { title, sort } = req.body;
    console.log(req.body); // print empty object

    await categoryModel.create({
      title,
      sort,
    });
    return res.status(200).send("created");
  } catch (err) {
    return res.status(400).send(err.message);
  }
};

Here is my code, when sending the request in JSON raw postman a response is what I need but when using a form-data it will return an empty body

this is happened when I send data with form-data returned an empty body enter image description here

this is happened when I send data with raw (Json) returned what i need enter image description here

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

0

If you use the option "x-www-form-urlencoded" I guess it will work. So here is the thing:

  • When you are using postman's "form-data" it will be considered as multipart/form-data for which you need a library to parse form-data such as multer.

  • For x-www-from-urlencoded raw data, you can parse the data using the express built-in middlewares or use a body-parser.

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