Create an HTTP task within an App Engine app
Stay organized with collections
Save and categorize content based on your preferences.
Creates an HTTP task within an App Engine application to trigger a Cloud Function.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Create an HTTP task within an App Engine app\n\nCreates an HTTP task within an App Engine application to trigger a Cloud Function.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Trigger Cloud Run functions using Cloud Tasks](/tasks/docs/tutorial-gcf)\n\nCode sample\n-----------\n\n### Node.js\n\n\nTo learn how to install and use the client library for Cloud Tasks, see\n[Cloud Tasks client libraries](/tasks/docs/reference/libraries).\n\n\nTo authenticate to Cloud Tasks, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const MAX_SCHEDULE_LIMIT = 30 * 60 * 60 * 24; // Represents 30 days in seconds.\n\n const createHttpTaskWithToken = async function (\n project = 'my-project-id', // Your GCP Project id\n queue = 'my-queue', // Name of your Queue\n location = 'us-central1', // The GCP region of your queue\n url = 'https://example.com/taskhandler', // The full url path that the request will be sent to\n email = '\u003cmember\u003e@\u003cproject-id\u003e.iam.gserviceaccount.com', // Cloud IAM service account\n payload = 'Hello, World!', // The task HTTP request body\n date = new Date() // Intended date to schedule task\n ) {\n // Imports the Google Cloud Tasks library.\n const {v2beta3} = require('https://cloud.google.com/nodejs/docs/reference/tasks/latest/overview.html');\n\n // Instantiates a client.\n const client = new v2beta3.https://cloud.google.com/nodejs/docs/reference/tasks/latest/tasks/v2beta3.cloudtasksclient.html();\n\n // Construct the fully qualified queue name.\n const parent = client.queuePath(project, location, queue);\n\n // Convert message to buffer.\n const convertedPayload = JSON.stringify(payload);\n const body = Buffer.from(convertedPayload).toString('base64');\n\n const task = {\n httpRequest: {\n httpMethod: 'POST',\n url,\n oidcToken: {\n serviceAccountEmail: email,\n audience: url,\n },\n headers: {\n 'Content-Type': 'application/json',\n },\n body,\n },\n };\n\n const convertedDate = new Date(date);\n const currentDate = new Date();\n\n // Schedule time can not be in the past.\n if (convertedDate \u003c currentDate) {\n console.error('Scheduled date in the past.');\n } else if (convertedDate \u003e currentDate) {\n const date_diff_in_seconds = (convertedDate - currentDate) / 1000;\n // Restrict schedule time to the 30 day maximum.\n if (date_diff_in_seconds \u003e MAX_SCHEDULE_LIMIT) {\n console.error('Schedule time is over 30 day maximum.');\n }\n // Construct future date in Unix time.\n const date_in_seconds =\n Math.min(date_diff_in_seconds, MAX_SCHEDULE_LIMIT) + Date.now() / 1000;\n // Add schedule time to request in Unix time using Timestamp structure.\n // https://googleapis.dev/nodejs/tasks/latest/google.protobuf.html#.Timestamp\n task.scheduleTime = {\n seconds: date_in_seconds,\n };\n }\n\n try {\n // Send create task request.\n const [response] = await client.createTask({parent, task});\n console.log(`Created task ${response.name}`);\n return response.name;\n } catch (error) {\n // Construct error for Stackdriver Error Reporting\n console.error(Error(error.message));\n }\n };\n\n module.exports = createHttpTaskWithToken;\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloud_tasks)."]]