【问题标题】:How to make an HTTP request in Cloud Functions for Firebase?如何在 Cloud Functions for Firebase 中发出 HTTP 请求?
【发布时间】:2017-09-15 03:10:12
【问题描述】:

我正在尝试使用 Cloud Functions for Firebase 调用苹果收据验证服务器。知道如何进行 HTTP 调用吗?

【问题讨论】:

    标签: node.js firebase google-cloud-functions firebase-cloud-functions


    【解决方案1】:

    答案是从 OP 的相关编辑中复制的


    OP 使用https://github.com/request/request 解决了这个问题

    var jsonObject = {
      'receipt-data': receiptData,
      password: functions.config().apple.iappassword
    };
    var jsonData = JSON.stringify(jsonObject);
    var firebaseRef = '/' + fbRefHelper.getUserPaymentInfo(currentUser);
    let url = "https://sandbox.itunes.apple.com/verifyReceipt"; //or production  
    request.post({
      headers: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url: url,
      body: jsonData
    }, function(error, response, body) {
      if (error) {
      } else {
        var jsonResponse = JSON.parse(body);
        if (jsonResponse.status === 0) {
          console.log('Recipt Valid!');
        } else {
          console.log('Recipt Invalid!.');
        }
        if (jsonResponse.status === 0 && jsonResponse.environment !== 'Sandbox') {
          console.log('Response is in Production!');
        }
        console.log('Done.');
      }
    });
    

    【讨论】:

      【解决方案2】:

      主要使用https://nodejs.org/api/https.html

      const http = require("http");
      const https = require('https');
      const mHostname ='www.yourdomain.info';
      const mPath     = '/path/file.php?mode=markers';
      
             const options = {
                     hostname: mHostname,
                     port: 80, // should be 443 if https
                     path: mPath ,
                     method: 'GET',
                     headers: {
                        'Content-Type': 'application/json'//; charset=utf-8',
                      }
             };
      
       var rData=""
             const req0 = http.request(options, (res0)=>
             {
                res0.setEncoding('utf8');
      
                res0.on('data',(d) =>{
                         rData+=d;
      
                 });
                 res0.on('end',function(){
                      console.log("got pack");
                      res.send("ok");
                    });
            }).on('error', (e) => {
                const err= "Got error:"+e.message;
                res.send(err);
            });
      req0.write("body");//to start request
      

      【讨论】:

      • 导入http模块为什么还要导入https模块?
      • 忘记删除
      【解决方案3】:

      请记住,您的 dependency footprint will affect deployment and cold-start times.以下是我使用https.get()functions.config() ping 其他功能支持的端点的方法。您也可以在调用 3rd 方服务时使用相同的方法。

      const functions = require('firebase-functions');
      const https = require('https');
      const info = functions.config().info;
      
      exports.cronHandler = functions.pubsub.topic('minutely-tick').onPublish((event) => {
          return new Promise((resolve, reject) => {
              const hostname = info.hostname;
              const pathname = info.pathname;
              let data = '';
              const request = https.get(`https://${hostname}${pathname}`, (res) => {
                  res.on('data', (d) => {
                      data += d;
                  });
                  res.on('end', resolve);
              });
              request.on('error', reject);
          });
      });
      

      【讨论】:

      • 为什么request.on不在https.get()回调中,为什么不使用response.on('error')
      猜你喜欢
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2019-05-16
      • 2018-04-19
      • 2018-03-04
      • 2017-10-31
      • 2017-12-18
      相关资源
      最近更新 更多