http post请求firebase云函数

http post请求firebase云函数,http,firebase,post,google-cloud-functions,Http,Firebase,Post,Google Cloud Functions,我想在firebase cloud函数中发出post请求并将数据发送到body。 默认情况下,它是get请求还是post请求 var functions = require('firebase-functions'); exports.tryfunction= functions.https.onRequest((req, res) => { console.log(req.body) // or it should be req.query }); 我如何知道并决定它是什

我想在firebase cloud函数中发出post请求并将数据发送到body。 默认情况下,它是get请求还是post请求

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

exports.tryfunction= functions.https.onRequest((req, res) => {
    console.log(req.body) // or it should be req.query 
});

我如何知道并决定它是什么方法?

没有默认值。请求方法是客户端选择发送的任何内容


回调中的
req
对象是一个express.js对象。使用链接的文档,您可以看到可以通过使用找到请求方法。

没有默认设置。请求方法是客户端选择发送的任何内容


回调中的
req
对象是一个express.js对象。使用链接的文档,您可以看到请求方法可以通过使用找到。

我只是分享简单的代码部分。我是这样用的

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


 exports.postmethod = functions.https.onRequest((request, response) => {
  if(request.method !== "POST"){
    response.send(405, 'HTTP Method ' +request.method+' not allowed');
    }

  response.send(request.body);
 });

我希望它会有帮助。

我只是分享了简单的代码部分。我是这样用的

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


 exports.postmethod = functions.https.onRequest((request, response) => {
  if(request.method !== "POST"){
    response.send(405, 'HTTP Method ' +request.method+' not allowed');
    }

  response.send(request.body);
 });

我希望它会有所帮助。

要指定Firebase函数接受的HTTP方法,您需要像在标准Express应用程序中通常那样使用Express API

您可以将full Express应用程序作为
onRequest()
的参数传递给HTTP函数。通过这种方式,您可以使用Express自己的API将Firebase函数限制为特定方法。下面是一个例子:

const express = require('express');
const app = express();

// Add middleware to authenticate requests or whatever you want here
app.use(myMiddleware);

// build multiple CRUD interfaces:
app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
app.post('/', (req, res) => res.send(Widgets.create()));
app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
app.get('/', (req, res) => res.send(Widgets.list()));

// Expose Express API as a single Cloud Function:
exports.widgets = functions.https.onRequest(app);
对上述代码的解释: 我们使用Express自己的API(例如,
app.post(…)
app.get(…)
)等)为不同的HTTP方法公开了一个具有端点
/widgets
的Firebase函数。然后,我们将
app
作为参数传递给
functions.https.onRequest(app)。就这样,你完了

如果您愿意,您甚至可以添加更多路径,例如,如果我们想要一个端点接受对端点的GET请求,该端点看起来像:
/widgets/foo/bar
,我们只需添加
app.GET('/foo/bar',(req,res=>{…});

这些都是直接从报纸上摘下来的。
我很惊讶@Doug Stevenson在他的回答中没有提到这一点。

要指定Firebase函数接受的HTTP方法,您需要像通常在标准Express应用程序中一样使用Express API

您可以将完整的Express应用程序作为
onRequest()
的参数传递给HTTP函数。这样,您可以使用Express自己的API将Firebase函数限制为特定方法。以下是一个示例:

const express = require('express');
const app = express();

// Add middleware to authenticate requests or whatever you want here
app.use(myMiddleware);

// build multiple CRUD interfaces:
app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
app.post('/', (req, res) => res.send(Widgets.create()));
app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
app.get('/', (req, res) => res.send(Widgets.list()));

// Expose Express API as a single Cloud Function:
exports.widgets = functions.https.onRequest(app);
对上述代码的解释: 我们使用Express自己的API,例如
app.post(…)
app.get(…)
,等等,为不同的HTTP方法公开了一个Firebase函数和端点
/widgets
,并将
app
作为参数传递给
functions.https.onRequest(app);
。就这样,完成了

如果您愿意,您甚至可以添加更多路径,例如,如果我们想要一个端点接受对端点的GET请求,该端点看起来像:
/widgets/foo/bar
,我们只需添加
app.GET('/foo/bar',(req,res=>{…});

这些都是直接从报纸上摘下来的。
我很惊讶@Doug Stevenson在他的回答中没有提到这一点。

@AdirZoari请查看我的回答。我描述了如何完全按照您的意愿行事。@AdirZoari请查看我的回答。我描述了如何完全按照您的意愿行事。