【问题标题】:How to do Axios request from Firebase Cloud Function如何从 Firebase Cloud Function 发出 Axios 请求
【发布时间】:2019-01-27 20:31:52
【问题描述】:

我在 Firebase Cloud Function 中尝试了以下方法来执行 Axios 请求,但没有成功。

    const functions = require('firebase-functions');
    const axios = require('axios');
    const cors = require('cors')({ origin: true });
    
    exports.checkIP = functions.https.onRequest((req, res) => {
        cors(req, res, () => {
            if( req.method !== "GET" ) {
                return res.status(401).json({
                    message: "Not allowed"
                });
            }
    
            return axios.get('https://api.ipify.org?format=json')
            .then(data => {
                console.log(data)
                res.status(200).json({
                    message: data.ip
                })
            })
            .catch(err => {
                res.status(500).json({
                    error: err
                })
            })
    
        })
    })

我还搜索了很多关于如何将 Axios 与 Cloud Functions 结合使用的示例,但没有找到。上面的代码没有返回任何东西。

谁能帮忙?

P.S.:我已经在我的 Firebase 帐户中添加了账单明细,并且没有使用免费的 Spark 计划,而是使用 Blaze 计划。

编辑:

我终于能够使用request-promise 节点包做到这一点,但仍然不知道如何使用axios 做到这一点。无论我尝试什么,axios 在 Firebase 云功能中都不起作用。这就是我所做的:

npm i --save cors request request-promise

然后这是我运行的代码:https://gist.github.com/isaumya/0081a9318e4f7723e0123f4def744a0e

也许它会帮助某人。如果有人知道如何使用Axios,请在下面回答。

【问题讨论】:

  • 您是否使用付费计划(Flame 或 Blaze)?如果不是,您将无法从 Cloud Function 调用外部服务。请参阅firebase.google.com/pricing“Spark 计划仅允许向 Google 拥有的服务发出出站网络请求。”
  • 是的,我是添加账单明细的付费计划。所以事实并非如此。
  • 尝试从您的请求处理程序返回一个承诺,例如return axios.get ...
  • 嗨@JamesPoag,试过但没用。
  • @JamesPoag 检查上面的更新代码。还是不行。

标签: firebase google-cloud-functions


【解决方案1】:

我将data.ip 更改为response.data.ip 并在两个res.status(... 行之前添加了return,当我尝试使用Axios 时,部署的云功能对我有用。

我的代码是

    const functions = require('firebase-functions');
    const axios = require('axios');
    const cors = require('cors')({ origin: true });

    exports.checkIP = functions.https.onRequest((req, res) => {
      cors(req, res, () => {
        if (req.method !== "GET") {
          return res.status(401).json({
            message: "Not allowed"
          });
        }
    
        return axios.get('https://api.ipify.org?format=json')
          .then(response => {
            console.log(response.data);
            return res.status(200).json({
              message: response.data.ip
            })
          })
          .catch(err => {
            return res.status(500).json({
              error: err
            })
          })
    
      })
    });

当我调用该函数时,我会得到一个回复​​,例如 { “消息”:“127.168.121.130” }

【讨论】:

  • 对于每个人都尝试在本地从命令行简单地运行一个示例:你必须将你的函数包装在 functions.https.onRequest().. 否则它将无法执行.. 我花了 30 分钟才弄清楚有多愚蠢我以为我可以运行一些东西:D
  • cors 是干什么用的?我可以不用吗?
  • 我在没有cors 的情况下尝试了这个例子,它仍然可以正常工作。
【解决方案2】:

我遇到了同样的问题。带有axios 的 HTTP 请求返回以下错误消息: TypeError: Converting circular structure to JSON

Here is an explanation of what is going on and how to get around this

您可以使用以下软件包: https://github.com/moll/json-stringify-safe

我不确定这种方法的一致性,我个人选择了request-promise,它比 axios 更重,但允许直接的 HTTP 请求。

【讨论】:

    猜你喜欢
    • 2018-09-13
    • 2018-07-28
    • 1970-01-01
    • 2021-07-13
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多