Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Triggering Cloud Function with Cloud Scheduler

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

Target: HTTP

URL: my trigger url for cloud function

HTTP method: POST

Body:

{
 "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.

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.

like image 281
Sergey Kravchenko Avatar asked Nov 08 '18 21:11

Sergey Kravchenko


People also ask

How do you trigger a cloud run?

In the Google Cloud console, go to Cloud Run. From the list of services, click an existing service, or create a new service. On the Service details page, click the Triggers tab. Click add_box Add Eventarc trigger.


2 Answers

Another way to solve the problem is this:

request.get_json(force=True)

It forces the parser to treat the payload as json, ingoring the Mimetype. Reference to the flask documentation is here

I think this is a bit more concise then the other solutions proposed.

like image 95
daymos Avatar answered Sep 20 '22 14:09

daymos



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


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.

Scenario - 1: Trigger the Cloud Function via Cloud Scheduler

  • Function fails to read the message in request body.

Scenario - 2: Trigger the Cloud Function via Test tab in Cloud Function interface

  • Function call always executes fine with no errors.

What did I find?

  • 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.

Solution

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

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

Cloud Scheduler - Content Type header issue

like image 21
Dinesh Palanivel Avatar answered Sep 22 '22 14:09

Dinesh Palanivel