0
votes

The relevant part of my app.js is as follows

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

var routes = require('./config/routes);
app.use('/', routes);

My route file is:

var express = require('express');
var router = express.Router();
var upgradesController = require('../../app/controllers/upgrades.server.controller');

// This should receive POST requests
router.post('/api/upgrades/device', upgradesController.create);

module.exports = router;

And finally my controller is

exports.create = function(req, res) {
    res.send(req.body);
}

But this sends nothing. It's always an empty JSON value. I'm using PostMan for testing:

enter image description here What is happening?

1
Considering it responds with the request's body, you'd need to show us what you're actually sending. - Yuri Zarubin
I'm just using Postman to send some data. See image above; I've updated my answer. - Kousha
When I send JSON to my API using Postman I need to send the data using the "raw" option and then select "JSON" from the dropdown so the "application/json" header is sent. Not sure if that's your issue but might be worth testing. - Sarus

1 Answers

2
votes

You're sending form-data, switch to x-www-form-urlencoded instead. You can also send "raw", and input valid JSON.