Reputation: 613
Trying to send a simple post request, the body
property is empty object {}
. Anyone knows why?
const compression = require('compression')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
app.set('port', process.env.PORT)
// Support JSON-encoded and encoded bodies.
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/api/' + APIVersion.latest() + '/post-job', (req, res) => {
console.log(req.body)
res.json({ hey: 'hey' })
})
// Start the server.
app.listen(app.get('port'), err => {
if (err) {
return console.log('There was an error', err)
}
console.log(`Server running on port ${app.get('port')}`)
})
Upvotes: 3
Views: 3682
Reputation: 86
If req.body isn't being populated, you'll need to check how you are sending your POST request, since req.body is completely separate from the URL. If it's in an HTML form:
<form action="/person" method="POST">
<input name="firstName" type="text">
<button>Submit</button>
</form>
If you're sending your requests through a different avenue, you'll have to check the documentation for that API.
Upvotes: 0
Reputation: 6360
req.params
is empty because your route doesn't specify any.
In Express
, params
refers to the route/url parameters. For example, if you wanted to have a route that allowed you to dynamically specify a user's last name, you may do:
app.get('/users/:last/info', (req, res) => {
// ....
})
This would give the route a parameter of last
, accessible by req.params.last
.
Upvotes: 4
Reputation: 22817
req.params
holds the route params, e.g. for /users/:id
:
GET /users/123
req.params.id = 123
You want to access req.query
, if you requested /api/v1/post-job?test=value
, or the body itself in case you posted any.
Upvotes: 0