Search code examples
javascriptnode.jsexpresshttpmiddleware

req.body returnig always empty in nodejs


const app = express();

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

app.post("/", (req, res) => {
    res.status(200).json(req.body);
});

sending empty object always

I am using Thunder-client. I am sending data through body form but it's always returning empty object

1


Solution

  • Content-type of Form is multipart/form-data and content-type of Form-encode is application/x-www-form-urlencoded. Does your data you want to post contain more than one type (like image along with text/plain)? If your data you want to post contains more than one type, look here. If it doesn't, add Content-type:application/x-www-form-urlencoded to the header and send post via Form-encoded option. Good Luck :)

    const app = express();
    
    app.use(express.urlencoded({ extended: true }));
    
    app.post("/", (req,res) => {
      res.status(200).json(req.body["random-data"]);
      // Result = lorem ipsum dolor sit amet
    });