1

I want pass array in my route POST

I Did try this

my object is

var myobj = [{name:'foo',name:'bar',name:'buz'}]

and I want pass these values in this http.request

var options = {
     hostname    : 'myhost',
     port        : 'myport',
     path        : 'myroute',
     method      : 'POST',
     agent       : false,
     body        : myobj,
     headers     : {'Content-Length': myobj.length}
 };

 var req = http.request(options,function(res) {});

router.post('myroute', function( req, res ){
    //I want myobj here
    console.dir(req.body); //EMPTY
})

I did try also

path        : 'myroute'+myobj and i recived socket hang up

I did try also

json = JSON.stringify(docs);
3
  • 1
    How can you post a body when Content-Length is 0? Commented May 9, 2014 at 13:44
  • @Bergi I've update...that was old code Commented May 9, 2014 at 13:48
  • myobj.length is 1. How do you expect the array to be serialized to a string? Commented May 9, 2014 at 13:51

1 Answer 1

1
req.write(JSON.stringify(myobj));

router.post('myroute', function( req, res ){
    console.dir(req.body);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.