javascript - node.js request POST array "first argument must be string or buffer" - Stack Overflow

I am trying to send an array from one node.js process (client) to another (server).My code on "cli

I am trying to send an array from one node.js process (client) to another (server).

My code on "client" node.js:

var express        = require('express');
var app            = express();
var config = require('./config');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var request     = require('request');

app.set('port', process.env.PORT || 3009);
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT

var arr = [{
        date : "2016/1/26",
        count: 6
    },
               {
        date : "2016/1/27",
        count: 0
    },
    {
        date : "2016/1/28",
        count: 0
    },
    {
        date : "2016/1/29",
        count: 0
    },
    {
        date : "2016/1/30",
        count: 0
    },
    {
        date : "2016/1/31",
        count: 2
    },
    {
        date : "2016/2/1",
        count: 0
    },
    {
        date : "2016/2/2",
        count: 4
    }];


       request.post({
            uri: config.URL,
            headers: {
                'Content-Type':'application/json;charset=UTF-8',
                'Accept-Encoding':'gzip, deflate',
                'X-Requested-With': 'XMLHttpRequest',
                'Accept':'application/json, text/plain, */*',
                'User-Agent': 'UserAgent' 
            },
            body: arr
            }, function(err, res, body){

                  //whatever
            });

On the server side I just receive the info and console.log it.

When launching the code, I am getting in Client: TypeError('first argument must be a string, Array, or Buffer');

Why can't I send arrays? I would swear I have done it 1000 times...


If I stringify the array in client like this: body: JSON.stringify(arr) and in server I try to parse it back to an array like this: var data = JSON.parse(req.body); I get the following error in the server when parsing the data:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at exports.uploadReads (C:\node\stockare2\server\panys\panys.controller.js:628:21)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at uploadUser (C:\node\stockare2\server\panys\panys.routes.js:117:7)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (C:\node\stockare2\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (C:\node\stockare2\node_modules\express\lib\router\index.js:330:12)
    at next (C:\node\stockare2\node_modules\express\lib\router\index.js:271:10)
    at serveStatic (C:\node\stockare2\node_modules\express\node_modules\serve-static\index.js:74:16)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\node\stockare2\node_modules\express\lib\router\index.js:312:13)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:280:7

If I console.log req.body before trying to parse the data, I can see the data is in a strange format:

{ '{"date":"2016/1/26......

I am trying to send an array from one node.js process (client) to another (server).

My code on "client" node.js:

var express        = require('express');
var app            = express();
var config = require('./config');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var request     = require('request');

app.set('port', process.env.PORT || 3009);
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT

var arr = [{
        date : "2016/1/26",
        count: 6
    },
               {
        date : "2016/1/27",
        count: 0
    },
    {
        date : "2016/1/28",
        count: 0
    },
    {
        date : "2016/1/29",
        count: 0
    },
    {
        date : "2016/1/30",
        count: 0
    },
    {
        date : "2016/1/31",
        count: 2
    },
    {
        date : "2016/2/1",
        count: 0
    },
    {
        date : "2016/2/2",
        count: 4
    }];


       request.post({
            uri: config.URL,
            headers: {
                'Content-Type':'application/json;charset=UTF-8',
                'Accept-Encoding':'gzip, deflate',
                'X-Requested-With': 'XMLHttpRequest',
                'Accept':'application/json, text/plain, */*',
                'User-Agent': 'UserAgent' 
            },
            body: arr
            }, function(err, res, body){

                  //whatever
            });

On the server side I just receive the info and console.log it.

When launching the code, I am getting in Client: TypeError('first argument must be a string, Array, or Buffer');

Why can't I send arrays? I would swear I have done it 1000 times...


If I stringify the array in client like this: body: JSON.stringify(arr) and in server I try to parse it back to an array like this: var data = JSON.parse(req.body); I get the following error in the server when parsing the data:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at exports.uploadReads (C:\node\stockare2\server\panys\panys.controller.js:628:21)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at uploadUser (C:\node\stockare2\server\panys\panys.routes.js:117:7)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (C:\node\stockare2\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (C:\node\stockare2\node_modules\express\lib\router\index.js:330:12)
    at next (C:\node\stockare2\node_modules\express\lib\router\index.js:271:10)
    at serveStatic (C:\node\stockare2\node_modules\express\node_modules\serve-static\index.js:74:16)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\node\stockare2\node_modules\express\lib\router\index.js:312:13)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:280:7

If I console.log req.body before trying to parse the data, I can see the data is in a strange format:

{ '{"date":"2016/1/26......
Share Improve this question asked Mar 29, 2016 at 16:59 EgidiEgidi 1,7769 gold badges45 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try to add the option json: true to the request:

request.post({
        uri: config.URL,
        headers: {
            'Content-Type':'application/json;charset=UTF-8',
            'Accept-Encoding':'gzip, deflate',
            'X-Requested-With': 'XMLHttpRequest',
            'Accept':'application/json, text/plain, */*',
            'User-Agent': 'UserAgent' 
        },
        json: true,
        body: arr
        }, function(err, res, body){

Use var data = eval(req.body) to convert into javascript object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250198a4618632.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信