I making a post request from my angular application to a node server. I'm using FormData and HttpClient to send the post.
const sendData = new FormData();
sendData.append('aff', loginName);
this.http.post(this.loginURL, sendData, { responseType: 'json' }).subscribe(data => {
console.log(data);
});
Here is my server side code:
var bodyParser = require('body-parser');
let socketIO = require('socket.io');
var server = http.createServer(app);
let io = socketIO(server);
const port = process.env.PORT || 3002;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse application/json
app.use(bodyParser.json());
app.post('/login', function (req, res) {
console.log(req.body);
res.status(200).send({ result: "OK" });
});
server.listen(port, () => {
console.log(`started on port: ${port}`);
});
When I console log the req.body I get {}. Here is a picture of the network:
I've tried a lot of different things. I'm not sure why this isn't working. I have it working on other websites. Please help!
UPDATE: this works for me.. but still curious as to why FormData isn't working properly...
const sendData = {'username': loginName};
const config = { headers: new HttpHeaders().set('Content-Type', 'application/json') };
this.http.post(this.loginURL, sendData, config).subscribe(data => {
console.log(data);
});