how to send Post request from express.js to another server ( java)?

Viewed 54167

I have to send data (json object) to another webserver (java).

This is my node.js code

var express = require('express');

var app = express();

app.get('/', function (req, res) {
    var data = querystring.stringify({
        username: "myname",
        password: " pass"
    });

    var options = {
        host: 'www.javaserver.com',
        port: 8070,
        path: '/login',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(data)
        }
    };

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

});

app.listen(8090);

This is not working. How can I do this?

2 Answers
Related