1

When I send a string value as the request, the req.body value is an object. I am using:

I have a factory cust1_service.postQuery:

.factory('cust1_service', function($http){
    return {
        postQuery : function(request){
            console.log('cust1 req : ' + request);
            console.log('typeof request : ' + typeof request);
            var config = {'Content-Type' : 'text/plain'};
            return $http.post('/cust1', request);
        }
    }

Here is how I call the factory in my controller:

cust1_service.postQuery(req_string).success(handleSuccess);

I am also using bodyParser.text() before my routes

var express = require('express'),   
config = require('./config/config'),    
bodyParser = require('body-parser'),    
api = require('./app/routes/api.js');               

var app = express();

app.use(bodyParser.text({   
    type: "text/*"               
}));                             

app.use(express.static(__dirname + '/public'));     //Serve static assets

require('./app/routes/api.js')(app, db);

app.listen(config.port, function() {    
    console.log('Listening on ' + config.port); 
})

So....when I get to my routing api

app.route('/cust1')
    .post(function(req,res){
            console.log('this is req.body : ' + req.body);

req.body is [object Object]...am I sending the request as a text type incorrectly?? I need req.body to be a string.

crayon_box
  • 61
  • 1
  • 7

1 Answers1

1

Try to "stringify" the req.body to be sure if it's still passed as a JSON object. You can also try logging the console like console.log('this is req.body : ', req.body, ' --- ');

One solution you can try is to completely remove the use of the BodyParser middleware - this should force the body to be text. SO you need to remove or comment out the line:

app.use(bodyParser.text({ type: "text/*"})) 

You can look at a closely related question here.

I hope this helps;

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32