var locationJSON, locationRequest;
locationJSON = {
latitude: 'mylat',
longitude: 'mylng'
};
locationRequest = {
host: 'localhost',
port: 1234,
path: '/',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
'content-length': locationJSON.length
}
};
var req;
req = http.request(options, function(res) {
var body;
body = '';
res.on('data', function(chunk) {
body += chunk;
});
return res.on('end', function() {
console.log(body);
callback(null, body);
});
});
req.on('error', function(err) {
callback(err);
});
req.write(data);
req.end();
On the other end, I have a node.js server listening to port 1234 and it never gets the request. Any ideas?
You are doing
req.write(data)but as far as I can see ‘data’ is not defined anywhere. You are also setting the ‘content-length’ header to locationJSON.length, which is undefined because locationJSON only has ‘latitude’ and ‘longitude’ properties.Properly define ‘data’, and change the ‘content-type’ and ‘content-length’ to use that instead.
Let me know if this still doesn’t work.