3

I'm trying to make http post request using the request module

text = 'some data';

request.post('http://example.com/read', {form:{data: text}});

This works fine for a simple string, but I need to be able to send an array or object.

When I try to read the array/object in the post handler the data property is empty.

What's going here? Your help will be much appreciated.

2
  • Post your server side code. Are you using perhaps the connect bodyParser middleware? If so, you want req.body.form.data to get at your text. Commented Sep 19, 2012 at 18:01
  • yes am using connect bodyParser middleware. I was only doing console.log(req.body) which gave me empty object but with req.body.form.data I get 'property data undefined. Commented Sep 19, 2012 at 18:14

1 Answer 1

8

Try this:

var request = require('request');

request({
  method: 'POST',
  uri: 'http://example.com/read',
  body: {'msg': 'secret'},
  json: true
}, function (error, response, body) {
  console.log('code: '+ response.statusCode);
  console.log(body);
})

Let me know if it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Perfect. I never wanted to use form anyway, just thought it was necessary. Thanks

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.