question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bodyParser.json with custom type application/*+json return an empty body

See original GitHub issue

I’m developing a HTTP server to interface with oneM2M. The header of the POST request received by oneM2M are:

content-type: 'application/vnd.onem2m-ntfy+json'
content-length: 441

Here the HTTP server code:

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

var app = express();
								
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json({
    type:'application/vnd.onem2m-ntfy+json'
}));
                                
app.listen(port);
console.log('SERVER LISTENING ON PORT ' + port);
console.log();
						
app.post('/', function(req, res) {
    console.log("REQUEST");
    console.log(req.body);
    console.log();
    res.status(200).send('OK');
});

The received body is empty. It is strange because in the request POST header content-length is not 0 and content-type defined for the bodyParser is the same in the POST header! Could you help me to solve this issue?

Kind regards, Luca

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lbarbieratocommented, Oct 12, 2017

Too old version

0reactions
dougwilsoncommented, Oct 12, 2017

P.S. if you’re using Express 4.16.0+ you don’t even need to require the body-parser separately, since it’s included now:

var express = require('express');
var port = 3331;

var app = express();
								
app.use(express.urlencoded({
    extended: false
}));
app.use(express.json({
    type:'application/vnd.onem2m-ntfy+json'
}));
                                
app.listen(port);
console.log('SERVER LISTENING ON PORT ' + port);
console.log();
						
app.post('/', function(req, res) {
    console.log("REQUEST");
    console.log(req.body);
    console.log();
    res.status(200).send('OK');
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

req.body empty on posts - node.js
You have to check whether the body-parser middleware is set properly to the type of request(json, urlencoded). If you have set, app.use(bodyParser.json());.
Read more >
Express body-parser middleware
Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
Read more >
req.body gets populated to empty object even when it ...
Expected behaviour. If the body was not parsed by some other parser before bodyParser.json , and if the current request is NOT to...
Read more >
req.body is empty in POST requests : r/node
I noticed that POST requests on my node server were not functioning properly (ie: not returning the expected data) so I tried a...
Read more >
Scala Body Parsers - 2.8.x
We can use any Scala type as the request body, for example String , NodeSeq , Array[Byte] , JsonValue , or java.io.File ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found