Create HTTP target tasks programmatically

Cloud Tasks handlers can be run on any HTTP endpoint with an external IP address such as GKE, Compute Engine, or even an on-premises web server. Your tasks can be executed on any of these services in a reliable, configurable fashion.

This document demonstrates how to programmatically create HTTP target tasks using Cloud Tasks client libraries, and place them in Cloud Tasks queues. You can also create HTTP target tasks in the Google Cloud console, by using the Google Cloud CLI, or by sending a direct request to the Cloud Tasks API. For more information, see Create tasks.

For tasks that have HTTP targets (as opposed to explicit App Engine targets, which are less common), there are two ways to create tasks:

  • CreateTask method: You must explicitly create a task object. Use this method if the tasks in your queue have different routing configurations. In this case, you specify routing at the task level and cannot use queue-level routing. This approach uses the CreateTask method.

  • BufferTask method: Use this method if your queue is set up to buffer tasks in front of a service. The queue must have queue-level routing. This approach uses the BufferTask method.

For guidance on how to choose between the two methods for creating HTTP tasks, see Choose your task creation method.

Create a task using the CreateTask method

This section discusses creating a task by constructing the task object. You use the CreateTask method.

When you create a task using the CreateTask method, you explicitly create and define the task object. You must specify the service and handler that process the task.

Optionally, you can pass task-specific data along to the handler. You can also fine-tune the configuration for the task, like scheduling a time in the future when it should be executed or limiting the number of times you want the task to be retried if it fails (see Advanced configuration).

The following examples call the CreateTask method to create a task using the Cloud Tasks client libraries.

C#


using Google.Cloud.Tasks.V2;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using System;

class CreateHttpTask
{
    public string CreateTask(
        string projectId = "YOUR-PROJECT-ID",
        string location = "us-central1",
        string queue = "my-queue",
        string url = "http://example.com/taskhandler",
        string payload = "Hello World!",
        int inSeconds = 0)
    {
        CloudTasksClient client = CloudTasksClient.Create();
        QueueName parent = new QueueName(projectId, location, queue);

        var response = client.CreateTask(new CreateTaskRequest
        {
            Parent = parent.ToString(),
            Task = new Task
            {
                HttpRequest = new HttpRequest
                {
                    HttpMethod = HttpMethod.Post,
                    Url = url,
                    Body = ByteString.CopyFromUtf8(payload)
                },
                ScheduleTime = Timestamp.FromDateTime(
                    DateTime.UtcNow.AddSeconds(inSeconds))
            }
        });

        Console.WriteLine($"Created Task {response.Name}");
        return response.Name;
    }
}

Go


// Command createHTTPtask constructs and adds a task to a Cloud Tasks Queue.
package main

import (
	"context"
	"fmt"

	cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
	taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
)

// createHTTPTask creates a new task with a HTTP target then adds it to a Queue.
func