#req.body in POST endpoint is undefined

16 messages · Page 1 of 1 (latest)

pliant tinsel
#

Hi, I've trying to write my own endpoint, GET request is fine but in POST method, the body in request is always undefined. Has anyone else encountered this?

teal drift
pliant tinsel
teal drift
#

I am not yet at this level of the knowledge but we can check it together 😄

pliant tinsel
#

It works, bro. Have a nice day ~

#

💪

teal drift
#

☮️ ❤️

mystic plaza
#

@pliant tinsel i am struggling with this same thing. how did you solve it?

strange scroll
#

You need to use bodyParser middleware, as shown in Naz's link above.

mystic plaza
#

unfortunately that isn't working for me

#
import express, { Router } from "express";


export default (rootDirectory: string, pluginOptions: any) => {
  const router = Router()
  router.use(express.json())
  router.use(bodyParser.json())

  router.post("/hello", (req, res) => {
    console.debug(req.body)
    res.json({"hello":"world"})
  });

  return router
};```
#

I am getting an empty body

#

uh. 🙂 nm

#

it is working

#

just ftr - don't need to use bodyParser. Just express.json() is enough