【发布时间】:2016-01-15 14:36:02
【问题描述】:
我正在使用 express.js 提交this api 的搜索查询。返回一个 json 对象。它包含一个指向 api 网站的图像 url。尝试获取图像时,返回 403 禁止。
这是一个示例url。它显示带有“拒绝访问”消息的 XML 错误。也许我需要在 url 中包含我的 api 密钥才能访问?如果是这样,我该怎么做?
这是我的代码:
var str = '';
var ingredients = null;
router.post('/', function(req,res){
ingredients = req.body.ingredients; //get value of an <input> named 'ingredients'
var callback = function(response){
str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved
response.on('end', function () {
var recipes = JSON.parse(str); //parse string to json
res.render('home', {recipes: recipes}); //render view with required object
});
}
var options = {
host: "food2fork.com",
path: "/api/search?key=[myKey]&q=" + ingredients
};
if(ingredients != null)
{
http.request(options,callback).end();
ingredients = null;
}
});
使用代码形式nodejitsu。
【问题讨论】:
标签: express http-status-code-403