Custom express route body is empty

default discord avatar
juliandm1995
2 months ago
6

Does anybody know why req.body is empty in my custom express route?



I'm doing a get request to /test. The request is being received correctly, but when I try to extract parameters from the Body, I get nothing

  • default discord avatar
    allannnc
    2 months ago

    are you sending anything in the body?

  • default discord avatar
    juliandm1995
    2 months ago

    Yes, I'm sending a json in the body. And the response is being received. I set that route in server.ts

    image.png
  • default discord avatar
    allannnc
    2 months ago

    do you have an

    app.use(express.json());

    somewhere in your server.ts?



    i would also try changing it to

    app.post

    to receive a

    req.body


    personally my get routes usually use URL/query params as opposed to a req body

  • default discord avatar
    juliandm1995
    2 months ago

    That worked!! Thanks 😄

  • default discord avatar
    allannnc
    2 months ago

    nice one!

  • default discord avatar
    juliandm1995
    2 months ago

    The app.use(express.json()); worked.



    And about the post, you are right, that is a better practice. This was just a test. In the real function code I'm using a post request. Thanks! 😄

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.