|
小程序云开发 如何调用第三方接口
首先安装 request-promise
npm 命令 npm install request-promise - // 云函数入口文件
- const cloud = require('wx-server-sdk')
- //npm install request-promise
- const rp = require('request-promise');
-
- cloud.init()
- // 云函数入口函数
- exports.main = async (event, context) => {
- //get
- const get_options = {
- method: 'GET',
- url: 'https://xxxxx.com',
- qs: {
- aa:'aa',
- bb:'bb',
- cc:'cc'
- },
- json: true
-
- };
-
- //获取get请求数据
- const get_res= await rp(get_options );
-
- //post
- const post_options = {
- method: 'POST',
- url: 'https://xxxx.com',
- body: {
- 'aa': "aa",
- 'bb': "bb",
- 'cc': "cc"
- },
- json: true
-
- };
- //获取post请求数据
- const post_res= await rp(post_options);
-
- return {get_res,post_res}
- }
复制代码
如果有header,加headers
- const post_options = {
- method: 'POST',
- url: 'https://xxxx.com',
- body: {
- 'aa': "aa",
- 'bb': "bb",
- 'cc': "cc"
- },
- headers:{cookie:'xxxx'},
- json: true
-
- };
复制代码
get 请求,加headers - const get_options = {
- method: 'GET',
- url: 'https://xxxxx.com',
- qs: {
- aa:'aa',
- bb:'bb',
- cc:'cc'
- },
- headers:{cookie:'xxxx'},
- json: true
-
- };
复制代码
|
|