0

I'm trying to fetch some data from a server using nodejs. I would like to send a POST data. There are two things that I would like to know about.

  1. How do I send the POST data?
  2. Whatever request I make using GET or POST, I always get a 400-BadRequest error. Have been searching through to solve this the whole day. Was not able to solve.

I'm sending the POST data now like request.write(JSON.stringify({key:"value"})); .. for which I'm always getting a 400 to whatever site I try this. Even on apache running at 127.0.0.1 on a php file that accepts the POST data.

Boopathi Rajaa
  • 4,659
  • 2
  • 31
  • 53
  • It's not unclear from your question who is sending a POST and who is receiving it? Are you posting a form to node.js and you want node to reply with content? Or are you trying to get node.js to post to another server? Also, please post your app.js or equivalent code. – Kato Dec 27 '11 at 20:22
  • Im trying to get nodejs to post to another server. (mentioned that im using http client.request in nodejs) – Boopathi Rajaa Dec 27 '11 at 21:22
  • Client.request just outputs to the page being rendered by node. – Kato Dec 27 '11 at 21:26
  • http://jsfiddle.net/BrYgF/ . The code that sends the request – Boopathi Rajaa Dec 27 '11 at 21:26

1 Answers1

1

This question was answered in another SO thread: How to make an HTTP POST request in node.js?

Essentially:

use require('http');

set 'Content-Type': 'application/x-www-form-urlencoded' in the options

In the callback, use res.on('data', ...) to transfer the posted info.

Community
  • 1
  • 1
Kato
  • 40,352
  • 6
  • 119
  • 149