【问题标题】:How Firebase Cloud functions handle HTTP post method?Firebase Cloud 函数如何处理 HTTP post 方法?
【发布时间】:2017-08-17 03:38:36
【问题描述】:

我创建了 Firebase Cloud Functions 应用, 我用https.onRequest 创建了函数。 并使用req.body 获取数据,但那里没有数据。

Firebase Cloud Functions 可以处理 HTTP POST 方法吗?

这是我的示例代码:-

var functions = require('firebase-functions');

exports.testPost = functions.https.onRequest((req, res) => {
    console.log(req.body);
});

我通过邮递员使用 POST 方法进行了测试,但没有在 Firebase 日志中显示结果。

【问题讨论】:

  • 云函数可以处理 POST。请显示您发送的请求正文,最好还显示您用于测试的 curl 命令行。
  • 你找到解决方案了吗?我想做同样的事情。
  • 总是终止你的函数。
  • 它有效。显示您的邮递员设置。

标签: firebase google-cloud-functions


【解决方案1】:

Firebase 函数支持 GET、POST、PUT、DELETE 和 OPTIONS 方法,您可以查看哪些方法会触发您的函数。

// Check for POST request
if(request.method !== "POST"){
 res.status(400).send('Please send a POST request');
 return;
}

然后从POST获取数据的请求(例如JSON类型)会在你的请求头中。

const postData = request.body;
// for instance
const format = req.body.format;
// query string params
let format = req.query.format;

【讨论】:

    【解决方案2】:

    在 Firebase 上构建的功能还可以使用 Express.js 路由器来处理 GET/POST/PUT/DELETE 等...谷歌完全支持,并且是实现这些类型功能的推荐方式。

    更多文档可以在这里找到:

    https://firebase.google.com/docs/functions/http-events

    这是一个基于 Node.js 构建的工作示例

    const functions = require('firebase-functions');
    const express = require('express');
    const cors = require('cors');
    const app = express();
    
    // Automatically allow cross-origin requests
    app.use(cors({ origin: true }));
    
    app.get('/hello', (req, res) => {
      res.end("Received GET request!");  
    });
    
    app.post('/hello', (req, res) => {
      res.end("Received POST request!");  
    });
    
    // Expose Express API as a single Cloud Function:
    exports.widgets = functions.https.onRequest(app);
    

    然后,运行 firebase deploy,它应该会编译您的代码并创建新的“小部件”功能。注意:您可以将小部件重命名为任何您想要的名称。最终,它将生成一个用于调用该函数的 URL。

    【讨论】:

    • functions.https.onCall() 真正适用于简单的“函数”类型调用。例如。 addMessage()、retrieveMessage()(所以不是很像 Rest)使用 Cloud Functions 客户端。使用functions.https.onRequest(),它允许更像Rest(发布、放置、删除方法)API,但您必须使用完整的客户端。看到这个答案 - stackoverflow.com/a/51477892/114549
    • 这很干净,但它直接在 firebase 中删除了所有不同 API 调用作为函数的概述......
    【解决方案3】:

    我打算做同样的事情。我认为方法应该是检查函数体中的request.method。一种可能的方法是:

    if (request.method != "POST") {
         respond.status(400).send("I am not happy");
         return;
    }
    
    // handle the post request
    

    这里有一些关于请求对象所持有的细节的参考:https://firebase.google.com/docs/functions/http-events

    【讨论】:

    • 前面的 OPTIONS 调用怎么样?
    【解决方案4】:

    也许您的项目尚未设置为与您的 Firebase 数据库进行通信。从您的终端尝试以下操作:

    npm install -g firebase-tools

    然后在您的项目文件夹中,运行以下命令并使用您的凭据登录

    firebase login

    然后

    firebase init functions

    这将创建一个包含 index.js、package.json 和 node_modules 的文件夹

    如果您正确使用 Postman,其余代码应该可以工作。

    【讨论】:

    • 问题是关于使用 Firebase 云功能处理发布请求。这与使用 firebase cli 登录 firebase 无关。
    猜你喜欢
    • 2014-05-18
    • 2021-04-27
    • 2019-06-07
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多