HTTP-Aufgabe in einer App Engine-Anwendung erstellen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Erstellt eine HTTP-Aufgabe in einer App Engine-Anwendung, um eine Cloud Function auszulösen.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]