Angular2 http.post and undefined req.body

11,644

You should set the appropriate headers since you are using JSON.

headers.append('Content-Type', 'application/json');
Share:
11,644
elzoy
Author by

elzoy

javascript/typescript/angular2/nodejs/express/mongodb/mongoose

Updated on July 03, 2022

Comments

  • elzoy
    elzoy almost 2 years

    I have a problem with receiving json from http.post. Let's make it clear a bit: This is my component file:

    import {Component} from 'angular2/core'
    import {Http, HTTP_PROVIDERS, Headers} from 'angular2/http'
    
    @Component({
      selector: 'login-form',
      template: `
        <form (ngSubmit)="onSubmit()">
    
          <button type="submit">Wyslij</button>
        </form>
      `
    })
    
    export class LoginFormComponent {
      constructor(public http: Http) { }
    
      onSubmit() {
        var headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
    
        var data = JSON.stringify({
          login: "zupa",
        });
    
        this.http.post('http://localhost:8080/send', data, {headers: headers})
        .subscribe();
      }
    }
    

    My server.js file

    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    
    var routes = require('./routes/index');
    
    var app = express();
    
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.use(express.static(path.join(__dirname, 'node_modules')));
    app.use(express.static(path.join(__dirname)));
    app.set('view engine', 'ejs');
    app.engine('html', require('ejs').renderFile);
    
    // uncomment after placing your favicon in /public
    //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    
    app.use('/', routes);
    
    app.get('*', function(req, res, next) {
      res.render("index.html", {title: 'Aplikacja'});
    });
    
    app.listen(8080, function() {
      console.log("Starting at localhost:8080");
    });
    
    module.exports = app;
    

    As you can see, this is a standard generated by express module server file.

    This is content of my "routes" file.

    var express = require('express');
    var router = express.Router();
    
    /* GET home page. */
    router.get('/', function(req, res, next) {
      res.render('index.html', {title: 'Infam'});
    });
    
    router.post('/send', function(req, res, next) {
      console.log("Received data: " + req.body.login);
    });
    
    module.exports = router;
    

    The server returns a message to me: "Received data: undefined" and I have no idea why the req.body.login is undefined. Can you help me? Thank you in advance.

    PS: The same is when I use

    "login": "zupa"
    

    instead of

    login: "zupa"