POST data with request module on Node.JS

Diego Torres picture Diego Torres · Jun 22, 2011 · Viewed 321.9k times · Source

This module is 'request https://github.com/mikeal/request

I think i'm following every step but i'm missing an argument..

var request = require('request');
request.post({
        url: 'http://localhost/test2.php',
         body: "mes=heydude"
         }, function(error, response, body){
            console.log(body);
    });

on the other end i have

echo $_POST['mes'];

And i know the php isn't wrong...

Answer

Diego Torres picture Diego Torres · Jun 22, 2011

EDIT: You should check out Needle. It does this for you and supports multipart data, and a lot more.

I figured out I was missing a header

var request = require('request');
request.post({
  headers: {'content-type' : 'application/x-www-form-urlencoded'},
  url:     'http://localhost/test2.php',
  body:    "mes=heydude"
}, function(error, response, body){
  console.log(body);
});