如何在Cloud Functions for Firebase中发出HTTP请求? [英] How to make an HTTP request in Cloud Functions for Firebase?

查看:54
本文介绍了如何在Cloud Functions for Firebase中发出HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a call to apples receipt verification server using Cloud Functions for Firebase. Any idea how to make an HTTP call?

推荐答案

请记住,您的依赖性占用空间将影响部署和冷启动时间.这是我使用 https.get()

Keep in mind that your dependency footprint will affect deployment and cold-start times. Here's how I use https.get() and functions.config() to ping other functions-backed endpoints. You can use the same approach when calling 3rd party services as well.

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

这篇关于如何在Cloud Functions for Firebase中发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆