您的位置:首页 > 移动开发 > 微信开发

微信小程序通过request-promise发送请求调用豆瓣API接口

2020-07-13 05:11 1426 查看

最近在学习微信小程序开发,需要调用豆瓣的API接口,可以在云函数中通过request-promise进行调用。

request-promise:https://github.com/request/request-promise

豆瓣API 接口:获取电影Top250

接口:https://api.douban.com/v2/movie/top250?apikey=0df993c66c0c636e29ecbb5344252a4a

访问参数:start : 数据的开始项;count:单页条数

如:https://api.douban.com/v2/movie/top250?apikey=0df993c66c0c636e29ecbb5344252a4a&start=0&count=10

调用步骤:

1、需要使用 npm 安装request,npm 集成在 Node.js 中的,安装参见文章:安装 Node.js 使用 npm

2、新建一个云函数,然后右键选择在终端中打开,输入如下命令,首先安装request,然后安装request-promise。

[code]npm install --save request
npm install --save request-promise

3、在 index.js 中调用

[code]var rp = require('request-promise');    // 引用request-promise

return rp('https://api.douban.com/v2/movie/top250?apikey=0df993c66c0c636e29ecbb5344252a4a&start=0&count=10')    // 请求某个网址
.then(function(res) {
return res;
})
.catch(function(err) {
console.err(err);
});

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: