云开发如何调用第三方api?非服务市场的api,比如聚合、百度之类的api
发布于 6 年前 作者 zjin 14312 次浏览 来自 官方Issues

云开发如何调用第三方api?非服务市场的api,比如聚合、百度之类的api

1 回复
首先安装  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'
     },
     jsontrue
   };

  //获取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"
       },
     jsontrue 
   };

  //获取post请求数据
  const post_res= await rp(post_options);
  return {get_res,post_res}
}
回到顶部