1

I wrote the following code:

var express = require('express');
var router = express.Router();
var app = express();

app.put('/data', function(req, res) {
    res.send("OK");
});

app.get('/data', function(req, res) {
    console.log();  // What to write here?
});
app.listen(4000);

I sent a PUT request by Postman: enter image description here

Now, I want to ask what I to write inside the brackets of console.log that the code will print the content stored in the http path?

I want to see the JSON

{"name":"John"}

EDIT: I tried the solution shown in this link: Node.js - get raw request body using Express:

var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require('body-parser');

app.use(function(req, res, next) {
    req.rawBody = '';
    req.on('data', function(chunk) {
        req.rawBody += chunk;
    });
    next();
});
app.use(bodyParser());

app.put('/data', function(req, res) {
    res.send("OK");
});

app.get('/data', function(req, res) {
    console.log(res.rawBody);
    res.end();
});

app.listen(4000);

I still didn't get what I expected.

Community
  • 1
  • 1
CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • Your first screenshot looks like you are already doing a PUT from postman. Maybe it's an issue with your server instead. – simonv Aug 07 '16 at 10:30
  • Possible duplicate of [Node.js - get raw request body using Express](http://stackoverflow.com/questions/18710225/node-js-get-raw-request-body-using-express) – Quentin Aug 07 '16 at 11:13
  • @Quentin, I tried it and it didn't work out. – CrazySynthax Aug 07 '16 at 11:38

0 Answers0