简体   繁体   中英

HTTP POST in Node.js not working

From nodejs i am trying to post data to another URL 127.0.0.1:3002 (in file poster.js), but when i try to access it on server at 127.0.0.1:3002 then posted data is not coming:

My poster.js looks like this:

var http = require('http');

function post() {

    var options = {
        host : '127.0.0.1',
        port : 3002,
        path : '/note/',
        method : 'POST'
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    req.write("<some>xml</some>");
    req.end();
}

post();

and my server code in app.js is:

var express=require('express');
app=express();

app.post('/note',function(req,res){
    console.log(req.params);
})

app.listen(3002);
console.log('sweety dolly')

my server console is showing:

sweety dolly
[]

req.params is showing [] that means it received nothing while sending i am sending xml

in two different command line i am firing two different processes like

 node app

and then in next command line

 node poster

What am i doing wrong????

Your client works, but when you POST , the data does not show up in params on the server by default (actually, params is for routing info)

Since you're posting raw data, you'll need to collect the data yourself to use it, for example by use ing your own simple body parser;

var express=require('express');

app=express();

app.use(function(req, res, next) {
  var data = '';
  req.setEncoding('utf8');
    req.on('data', function(part) {      // while there is incoming data,
       data += part;                     // collect parts in `data` variable
    }); 

    req.on('end', function() {           // when request is done,
        req.raw_body = data;                 // save collected data in req.body
        next();
    });
});

app.post('/note',function(req,res){
    console.log(req.raw_body);               // use req.body that we set above here
})

app.listen(3002);
console.log('sweety dolly')

EDIT: If you want the data as a parameter, you'll need to change the client to post the data as a querystring with a name for the data;

var http = require('http');
var querystring = require('querystring');

function post() {

    var post_data = querystring.stringify({ xmldata: '<some>xml</some>' })

    var options = {
        host : '127.0.0.1', port : 3002, path : '/note/', method : 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': post_data.length
        }
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });


    req.write(post_data);
    req.end();
}

post();

Then you can just use the standard bodyParser to get the data from the param function;

var express=require('express');

app=express();

app.use(express.bodyParser());

app.post('/note',function(req,res){
    console.log(req.param('xmldata'));
})

app.listen(3002);
console.log('sweety dolly')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM