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

    image.png
  • 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 help straight from the Payload team with an Enterprise License.