I am trying to schedule a google cloud task to recur once after a specified period of time (say 60 seconds) using the following code (with the required project info).
async function scheduele() {
const expiresIn = 60
const expirationAtSeconds = TIME / 1000 + expiresIn
const project = JSON.parse(process.env.FIREBASE_CONFIG).projectId
const location = LOCATION
const queue = QUEUE_NAME
const tasksClient = new CloudTasksClient()
const parent = await tasksClient.queuePath(project, location, queue)
const url = "https://" + location + "-" + project + ".cloudfunctions.net/funcA"
const base64String = Buffer.from(JSON.stringify(PAYLOAD)).toString('base64')
const task = {
"httpRequest": {
"httpMethod": "POST",
"url": url,
base64String,
"headers": {
"Content-Type": "application/json"
},
},
}
task.scheduleTime = {
seconds: expirationAtSeconds,
};
const request = {parent, task}
const [response] = await tasksClient.createTask(request);
return Promise.resolve({task: response.name})
}
I am trying to get this to call a return handler funcA
exports.funcA = functions.https.onRequest(async (req, res) => {
console.log(req.body)
try {
res.status(200).send("ok.")
return;
} catch(error) {
res.status(error.code).send(error.message);
console.error(error.message)
}
})
I am able to call the function using a JSON from the google cloud scheduler console, however, every time I trigger scheduele(), I get an unexpected token "-" in JSON error before console.log(req.body) even prints. I am unsure why I can call the function with no problem from the dashboard, but when I use the same JSON and trigger it from a cloud function, I get this error.
Thanks!
P.S. I have checked that the queue exists, npm packages are all latest version, etc.
Edit:
PROMISE is a JSON with one entry along the lines of {"key": "somestring"}. I also was able to successfully invoke the function submitting the following JSON in the google cloud function tester.
"httpRequest":{
"httpMethod":"POST",
"url":"functionURL",
"body":"LU1HUE4tZ3JBMlhqV3pXR0hrd0s=",
"headers":{
"Content-Type":"application/json"
}
},
"scheduleTime":{
"seconds":1599425744.055
}
}
The exact error is:
SyntaxError: Unexpected token - in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (/layers/google.nodejs.functions-framework/functions-framework/node_modules/body-parser/lib/types/json.js:158:10)
at parse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/body-parser/lib/types/json.js:83:15)