Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Custom express route body is empty

default discord avatar
juliandm1995last year
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
    allannnclast year

    are you sending anything in the body?

  • default discord avatar
    juliandm1995last year

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

  • default discord avatar
    allannnclast year

    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
    juliandm1995last year

    That worked!! Thanks 😄

  • default discord avatar
    allannnclast year

    nice one!

  • default discord avatar
    juliandm1995last year

    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! 😄

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.