我使用/ api/authenticate endpoint创建了一个node.js服务。我可以使用'用户名'和'密码'作为输入(主体参数)从POSTMAN成功地调用这项服务。如何从另一个node.js服务器调用相同的服务?在node.js应用程序中发出发布请求
有了邮递员,我得到,
body: {name: 'xxxxxx', password: 'xxxxxx' }
headers: { 'content-type': 'application/x-www-form-urlencoded',
host: 'xx.xx.xx.xx:xxxx',
connection: 'close',
'content-length': '0' }
POST/API /认证200 1.336毫秒 - 72
以下是另一个应用程序的NodeJS ......这使得成功的请求呼叫,但没有任何身份参数(用户名和密码)到达认证服务器api时。
var my_http = require('http');
app.get('/makeacall', function(req, res) {
var output = '';
var options = {
body: { name: 'xxxxxx', password: 'xxxxxx' },
method: 'POST',
host: 'xx.xx.xx.xx',
port: 'xxxx',
path: '/api/authenticate',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
console.log('before request');
var req = my_http.request(options, function(response) {
console.log('response is: ' + response);
console.log('Response status code: ' + response.statusCode);
response.on('data', function(chunk) {
console.log('Data ..');
output += chunk;
});
response.on('end', function(chunk) {
console.log('Whole Data ..' + output);
});
});
req.on('error', function(err) {
console.log('Error: ' + err);
});
req.end();
console.log('444');
res.send({ message: 'View record message'});
});
从此nodejs应用程序,我得到服务器上的空主体。
body: {}
headers: { 'content-type': 'application/x-www-form-urlencoded',
host: 'xx.xx.xx.xx:xxxx',
connection: 'close',
'content-length': '0' }
POST /api/authenticate 200 1.336 ms - 72
我在想什么?任何帮助表示赞赏。