Kingsley Akindele
Kingsley Akindele

Reputation: 361

express.json() not helping in getting my request body params

I have the following in my app.ts, but I am trying to access the body of my request, which returns undefined.

import { Request, Response, NextFunction, Application } from 'express';
import express from 'express'
import chatRouter from './routes/chats.routes';
import  CustomError  from './exceptions/custom-error';

const app: Application = express();

// access body params especially when making a post/put request
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const apiLevelMiddleware = (req: Request, res: Response, next: NextFunction) => {
    console.log('Request URL:', req.originalUrl, req.body);
    next()
}

basically, req.body is an empty object {}.

my postman call looks like this..

Postman request what am I not doing correctly??

Upvotes: 1

Views: 1076

Answers (1)

Heiko Theißen
Heiko Theißen

Reputation: 16728

You have registered middlewares for parsing request bodies with Content-Type: application/x-www-form-urlencoded and Content-Type: application/json, but to parse Content-Type: multipart/form-data (which, according to your screenshot, you use), you need another middleware, for example, multer.

Upvotes: 1

Related Questions