Chaim Ochs
Chaim Ochs

Reputation: 129

When an object is sent along with a get/post request express returns an empty object for req.body

When I send an object as a parameter of my post or get request express doesn't seem to receive it

I have tried get and post requests both on the front end and on the server. All the dependencies are working fine (body-parser, etc.)

front end:

axios.get('http://localhost:4000/videoComments/comment', {pauseTime: 10})

or

axios.get('http://localhost:4000/videoComments/comment', {data:{pauseTime: 10}})

back-end:

videoCommentsRoutes.route('/comment').get(function (req, res) {
    console.log(req.body);

req.body is an empty object. req.data, req.params are all undefined

Upvotes: 1

Views: 63

Answers (2)

Revansiddh
Revansiddh

Reputation: 3062

Back-End should be like

videoCommentsRoutes.route('/comment/:pauseTime').get(function (req, res) {
    console.log(req.params.pauseTime);
})

or

videoCommentsRoutes.route('/comment').get(function (req, res) {
        console.log(req.query.pauseTime);
    })

Front-end Call like

axios.get('http://localhost:4000/videoComments/comment', {params:{pauseTime: 10}})

Upvotes: 0

Limbo
Limbo

Reputation: 2290

GET request supports only query parameters. axios (as well as any of fetch or XMLHTTPRequest wrappers, such as superagent) should transform your object into query string.

Try using req.query to get the query parameters. Here is express docs about it.

Upvotes: 1

Related Questions