Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js : POST data as KEY of a req.body object instead of VALUE of req.body?

from the client I'm doing :

$.ajax({
        url: '/create',
        type: 'POST',
        data: JSON.stringify({
            theme: "somevalue",
            snippet: {
                name: "somename",
                content: "somevalue"
            }
        }), 
        complete: function (response)
        {

        }
    });

on the server ( node.js/express.js ) I'm doing :

var app = express();
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
.......
... 
app.post('/create', function (req, res)
{
   var dataReceived = req.body;
});

I expected the value of dataReceived to be :

{
   "theme" : "somevalue",
   "snippet" : {
     "name": "somename",
     "content" : "somevalue"
   } 
}

Instead the value of dataReceived was :

{ 
 '{"theme":"somevalue","snippet":"name":"somename","content":"somevalue"}}': '' 
}

This is really weird and I can't find what I'm doing wrong. Any ideas?

from the BodyParser module documentation :

bodyParser.urlencoded(options)

Returns middleware that only parses urlencoded bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).

Is this related to my problem ?

like image 655
Sprout Coder Avatar asked Oct 18 '15 18:10

Sprout Coder


People also ask

How do I get a body from express request?

Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.

What does bodyParser do in Express?

What Is Body-parser? Express body-parser is an npm module used to process data sent in an HTTP request body. It provides four express middleware for parsing JSON, Text, URL-encoded, and raw data sets over an HTTP request body.

IS REQ A body object?

In express, you will get req. body property. Basically, it is an object that will allow you to access data from the client-side in the form of a string or JSON Object.


2 Answers

Remove Stringify in your client side

$.ajax({
        url: '/create',
        type: 'POST',
        data: {
            theme: "somevalue",
            snippet: {
                name: "somename",
                content: "somevalue"
            }
        }, 
        complete: function (response)
        {

        }
    });

or parse it again in server side

app.post('/create', function (req, res)
{
   var dataReceived = JSON.parse(req.body);
});
like image 126
Samundra Khatri Avatar answered Sep 20 '22 18:09

Samundra Khatri


Set this content-type on client side ajax call if you are going to use JSON.stringify:

contentType: "application/json"
like image 20
Amith Avatar answered Sep 23 '22 18:09

Amith