How to force parse request body as plain text instead of json in Express?

I am using nodejs + Express (v3) like this:

app.route('/some/route', function(req, res) {
  var text = req.body; // I expect text to be a string but it is a JSON

I checked the request headers and the content-type is missing. Even if "Content-Type" is "text/plain" it is parsing as a JSON it seems. Is there anyway to tell the middleware to always parse the body as a plain text string instead of json? Earlier versions of req used to have req.rawBody that would get around this issue but now it does not anymore. What is the easiest way to force parse body as plain text/string in Express?


If you remove the use of the bodyParser() middleware, it should be text. You can view the bodyParser docs for more info:

Remove this line:


Looks like you're right. You can create your own rawBody middleware in the meantime. However, you still need to disable the bodyParser(). Note: req.body will still be undefined.

Here is a demo:


var express = require('express')
  , http = require('http')
  , path = require('path')
  , util = require('util');

var app = express();

function rawBody(req, res, next) {
  req.rawBody = '';
  req.on('data', function(chunk) {
    req.rawBody += chunk;
  req.on('end', function(){

  app.set('port', process.env.PORT || 3000);
});'/test', function(req, res) {
  console.log('RB: ' + req.rawBody);
  console.log('B: ' + JSON.stringify(req.body));
  res.send('got it');

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));


var request = require('request');

  method: 'POST',
  uri: 'http://localhost:3000/test',
  body: {'msg': 'secret'},
  json: true
}, function (error, response, body) {
  console.log('code: '+ response.statusCode);

Hope this helps.

Need Your Help

Prevent blank page on last side in xsl fo

xslt xslt-1.0 xsl-fo fop apache-fop

i'm using xsl fo and apache fop to generate pdf documents. i have different fo page-sequences (coverpage, toc, imprint and the "main content").

Adding a Object to End of a Linked List in One Class

java class methods linked-list

I am a bit new to LinkedList and I want to practice by making methods in ExampleLinkedList class. There is a list in test3. When I call test3 I get Goodbye Thanks Hello. What I want is to add "Adde...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.