如何在Cloud Functions for Cloudbase中发出HTTP请求?

时间:2017-04-19 04:28:28

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

我正在尝试使用Cloud Functions for Firebase拨打Apple收据验证服务器。知道如何进行HTTP呼叫吗?

3 个答案:

答案 0 :(得分:3)

从OP的问题编辑中复制答案

OP使用https://github.com/request/request

解决了这个问题
string sql = "select * from test";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand cmd = new MySqlCommand(sql, conn);
conn.open
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
  q += "<tr><td id='" + reader.GetString("id") + "'>" + reader.GetString("name") + "</td></tr>\r\n";

    if (reader.GetString("name") == "test")
   {
    // This will not work - I need to reference the actual ID - how can I get this to work?
    reader.GetString("id").Attributes.CssStyle.Add("background-color", "#e1e1e1");
   }
}

答案 1 :(得分:3)

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

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);
    });
});

答案 2 :(得分:1)

主要使用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