我试图https://api-mean.herokuapp.com/api/contacts用以下数据发送邮件调用:
{
"name": "Test",
"email": "test@xxxx.in",
"phone": "989898xxxx"
}
Run Code Online (Sandbox Code Playgroud)
但没有得到任何回应.我也尝试过邮差它工作正常.我在邮递员中得到回复.
我使用以下nodejs代码:
var postData = querystring.stringify({
"name": "Test",
"email": "test@xxxx.in",
"phone": "989898xxxx"
});
var options = {
hostname: 'https://api-mean.herokuapp.com',
path: '/api/contacts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var req = http.request(options, function (res) {
var output = '';
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function () {
var obj = JSON.parse(output.trim());
console.log('working = ', obj);
resolve(obj);
});
});
req.on('error', function (e) {
console.error(e);
});
req.write(postData);
req.end();
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
如何从节点服务器发送http post调用?
我建议您使用请求模块使事情变得更容易.
var request=require('request');
var json = {
"name": "Test",
"email": "test@xxxx.in",
"phone": "989898xxxx"
};
var options = {
url: 'https://api-mean.herokuapp.com/api/contacts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: json
};
request(options, function(err, res, body) {
if (res && (res.statusCode === 200 || res.statusCode === 201)) {
console.log(body);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13396 次 |
| 最近记录: |