Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

nodejs request的post报错

function getAccessTokenls (req, res) {
  const resObj = Common.clone(Constant.DEFAULT_SUCCESS); // 定义好的成功返回模板
  const appId = CONFIG.WECHAT.APP_ID;
  const appSecret = CONFIG.WECHAT.APP_SECRET;
  let tasks = {
    /**
     * 获取access_token
     */
    accessToken: cb => {
      request({
        url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`,
        method: 'GET',
        headers: { 'Content-Type': 'text/json' }
      }, function (error, response, body) {
        // console.log(body)
        if (!error && response.statusCode == 200) {
          let data = JSON.parse(body)
          resObj.data.accessTokenData = data;
          cb(null, data)
        }
      });
    },
    /**
     * 通过access_token获取ticket(临时)
     */
    ticket: ['accessToken', (result, cb) => {
      // console.log(result)
      let accessToken = result.accessToken.access_token;
      let url = `https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${accessToken}`;
      request({
        url: url,
        method: 'POST',
        json: true,
        headers: { 'Content-Type': 'text/json' },
        body: {
          expire_seconds: 60 * 60 * 24 * 7, // 有效时间(单位s)
          action_name: "QR_STR_SCENE", // 临时
          action_info: {
            scene: {
              scene_str: "张三"
            }
          }
        }
      }, function (error, response, body) {
        // console.log(body)
        if (!error && response.statusCode == 200) {
          let data = JSON.parse(body)
          resObj.data.ticketData = data;
          resObj.data.ticketData.type = "临时";
          cb(null)
        }
      });
    }]
  }
  Common.autoFn(tasks, res, resObj); // 异步执行
}

一开始body那写的是data,报错
` "errcode": 44002,

        "errmsg": "empty post data rid: 5f44c53b-6c3d44e5-1aeb35c6",`

百度方法后改成body,接口直接报502错误,没有往下走了,是哪里出了问题呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

request 已经是一个被废弃的包了,改用 axios 吧


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...