我是nodejs以及开发人员的新手。 我正在尝试从JSON格式的营养网站获取一组数据蝙蝠。如果我用我的应用程序和api键制定url以及粘贴到浏览器的标准,我就可以得到一个JSON数据。当我尝试发送POST请求时,网站会询问请求何时返回,它说它无法找到网址。它是什么做的是安装“:443”到主机URL的末尾,就像我说回来为错误:POST请求无法找到网址
Error: getaddrinfo ENOTFOUND https://api.nutritionix.com/v1_1/search https://api.nutritionix.com/v1_1/search:443
我想要做的是之后的URL到底是追加'发布数据'。
这里是我的代码:
var https = require('https');
var querystring = require('querystring');
var postData = { // Nutrionix required JSON formt
"appId":"MY_APP_KEY",
"appKey":"MY_API_KEY",
"query": "Lentils",
"fields": ["item_name", "nf_calories", "nf_serving_size_qty", "nf_serving_size_unit"],
"sort":{
"field":"score",
"order":"desc"
},
"filters":{
"item_type":"2"
}
};
console.log("This is header dta" + postData);
postBody = querystring.stringify(postData);
var post_options = {
host:"https://api.nutritionix.com/v1_1/search",
"port":"443",
method:"post",
"path":"/",
headers:{"Content-Type":"application/json",
'Content-Length': postBody.length
}
}
console.log(post_options);
var request = https.request(post_options,function(response){
return response;
});
我也是通过这个数据到开发HTTP插件在Chrome和取回适当的反应。
任何帮助,将不胜感激。
主持人: “https://api.nutritionix.com/v1_1/search”, “端口”: “443”, AREN”你在post_options中提到这个吗? –
@PratikGaikwad我是,但即使我删除它,我仍然得到同样的错误。它将:443放在最后,而不是postData。我补充说,因为我认为它需要在那里。将URL粘贴到浏览器中当然会返回没有数据的网站并要求提供API密钥,但是如果我在末尾放置:443,则会得到'资源无法找到。 –
你可以试试这个吗?主持人: “https://api.nutritionix.com/v1_1/search/”。也从post_options中删除443 –