使用Cloud Scheduler的HTTP触发云功能 [英] HTTP Triggering Cloud Function with Cloud Scheduler

查看:73
本文介绍了使用Cloud Scheduler的HTTP触发云功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cloud Scheduler中执行我的云功能时遇到问题.我用下一个参数创建了作业:

I have a problem with a job in the Cloud Scheduler for my cloud function. I created the job with next parameters:

目标:HTTP

URL :我针对云功能的触发URL

URL: my trigger url for cloud function

HTTP方法:POST

身体:

{
 "expertsender": {
  "apiKey": "ExprtSender API key",
  "apiAddress": "ExpertSender APIv2 address",
  "date": "YYYY-MM-DD",
  "entities": [
     {
        "entity": "Messages"
     },
     {
        "entity": "Activities",
        "types":[
           "Subscriptions"
        ]
     }
  ]
 },
 "bq": {
         "project_id": "YOUR GCP PROJECT",
         "dataset_id": "YOUR DATASET NAME",
         "location": "US"
       } 
}

此主体中的实际值已更改.

The real values has been changed in this body.

运行此作业时出现错误.原因是由于POST请求中的处理主体引起的.

When I run this job I got an error. The reason is caused by processing body from POST request.

但是,当我使用此主体并将其用作测试"中的触发事件时,我没有得到任何错误.所以我认为,这个问题代表了我的工作,但我不知道该如何解决.任何想法我都会很高兴.

However, when I take this body and use it as Triggering event in Testing I don't get any errors. So I think, that problem in body representation for my job but I havn't any idea how fix it. I'll be very happy for any idea.

推荐答案


免责声明: 我已经尝试使用NodeJS解决相同的问题,并且能够找到解决方案


Disclaimer: I have tried to solve the same issue using NodeJS and I'm able to get a solution

我知道这是一个老问题.但是我觉得值得回答这个问题,因为我花了将近2个小时来弄清楚这个问题的答案.

I understand that this is an old question. But I felt like its worth to answer this question as I have spent almost 2 hours figuring out the answer for this issue.

场景-1:通过Cloud Scheduler触发Cloud功能

  • 函数无法读取请求正文中的消息.

方案-2:通过云功能"界面中的测试"选项卡触发云功能

  • 函数调用始终可以正常执行,没有错误.

我找到了什么?

  • 通过Cloud Scheduler执行GCF例程时,它将标头content-type作为application/octet-stream发送.当Cloud Scheduler POST数据时,这使express js无法解析请求正文中的数据.
  • 但是,当使用完全相同的请求主体通过Cloud Function接口测试功能时,一切工作正常,因为接口上的 Testing 功能将标头content-type发送为application/json,并且express js能够读取请求正文并将数据解析为JSON对象.
  • When the GCF routine is executed via Cloud Scheduler, it sends the header content-type as application/octet-stream. This makes express js unable to parse the data in request body when Cloud scheduler POSTs the data.
  • But when the exact same request body is used to test the function via the Cloud Function interface, everything works fine because the Testing feature on the interface sends the header content-type as application/json and express js is able to read the request body and parses the data as a JSON object.

解决方案

我必须手动将请求正文解析为JSON(明确使用基于内容类型标头的if条件)来获取请求正文中的数据.

I had to manually parse the request body as JSON (explicitly using if condition based on the content-type header) to get hold of data in the request body.

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';

  console.log('Headers from request: ' + JSON.stringify(req.headers));

  let parsedBody;

  if(req.header('content-type') === 'application/json') {
    console.log('request header content-type is application/json and auto parsing the req body as json');
    parsedBody = req.body; 
  } else {
    console.log('request header content-type is NOT application/json and MANUALLY parsing the req body as json');
    parsedBody = JSON.parse(req.body);
  }

  console.log('Message from parsed json body is:' + parsedBody.message);

  res.status(200).send(message);
};

这确实是一个功能问题,Google必须解决,并希望Google尽快解决.

It is truly a feature issue which Google has to address and hopefully Google fixes it soon.

Cloud Scheduler-内容类型标头问题

这篇关于使用Cloud Scheduler的HTTP触发云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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