云开发使用 got 的 get/post 传参请求示例代码

303 篇文章 53 订阅
28 篇文章 6 订阅

使用 got 进行网络请求的步骤:

1.创建云函数,并在终端执行云函数

2.执行 npm 安装 got ,命令:cnpm install --save got

3.在云函数中使用

示例代码:

// 云函数入口文件
const cloud = require('wx-server-sdk')
const got = require('got');

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {

  const page = event.page
  
  //appid和秘钥
  const appid = 'xxxx',secret = 'xxxx';

  const resultValue_options = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      appid,
      secret,
      grant_type: 'client_credential'
    })
  };
  var url1 = 'https://api.weixin.qq.com/cgi-bin/token?appid=' + appid + '&secret=' + secret + '&grant_type=client_credential'

  const body = await got(url1).json();
  const token = body.access_token;

  var url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token;

  //获取小程序码配置
  const code_options = 
  {
    methods: 'post',
    body: JSON.stringify({
      'width': 200,
      'scene': 'scene_' + new Date().getTime()
    })
  }
  //获取二进制图片
  const buffer = await got.post(url, code_options);


  const upload = await cloud.uploadFile({
    cloudPath: 'code/' + new Date().getTime()+'.png',
    fileContent: buffer,
  })


  return {
    wxacodefileID: upload.fileID
  }

}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a_靖

对你有帮助吗?打赏鼓励一下?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值