# Add an HTTP target task to a Cloud Tasks queue

Cloud Tasks lets you dispatch asynchronous tasks to HTTP targets while
controlling execution rates and retry behavior. In this quickstart, you use the
Cloud Tasks API to create a queue and add an
[HTTP target task](https://docs.cloud.google.com/tasks/docs/dual-overview.md.txt#http) to that queue.

## Before you begin

<br />

## Create a Cloud Tasks queue

Use the [`gcloud tasks queues create`](https://docs.cloud.google.com/sdk/gcloud/reference/tasks/queues/create)
command to create your queue.

> [!CAUTION]
> **Caution:** Using Cloud Tasks `gcloud` queue management methods carries some risk if used together with methods based on uploading `queue.yaml/queue.xml`. For more information, see [Use Queue Management or queue.yaml](https://docs.cloud.google.com/tasks/docs/queue-yaml.md.txt).

1. In your terminal, create a queue that logs all operations.

   ```bash
   gcloud tasks queues create QUEUE_NAME \
       --log-sampling-ratio=1.0 \
       --location=REGION
   ```

   Replace the following:
   - `QUEUE_NAME`: a name for your Cloud Tasks queue
   - `REGION`: the [supported region](https://docs.cloud.google.com/tasks/docs/locations.md.txt) you deployed your service or app in
2. Wait for the queue to initialize and then verify that it was created
   successfully.

   ```bash
   gcloud tasks queues describe QUEUE_NAME \
       --location=REGION
   ```

   The output should be similar to the following:

        name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
        rateLimits:
          maxBurstSize: 100
          maxConcurrentDispatches: 1000
          maxDispatchesPerSecond: 500.0
        retryConfig:
          maxAttempts: 100
          maxBackoff: 3600s
          maxDoublings: 16
          minBackoff: 0.100s
        state: RUNNING

## Add a task to the Cloud Tasks queue

Use the
[`gcloud tasks create-http-task`](https://docs.cloud.google.com/sdk/gcloud/reference/tasks/create-http-task)
command to create a task that targets an HTTP endpoint and add the task to your
queue.

1. Create a task, add it to the queue you created, and deliver that task to an
   HTTP endpoint.

   ```bash
   gcloud tasks create-http-task \
       --queue=QUEUE_NAME \
       --url=URL_PATH \
       --method=GET \
       --location=REGION \
       --project=PROJECT_ID
   ```

   Replace `URL_PATH` with the full URL path that the
   request will be sent to. The path must begin with either `http://` or
   `https://`---for example: `https://www.example.com`.

   > [!NOTE]
   > **Note:** To target a Cloud Run service or function, use its [default `run.app` URL](https://docs.cloud.google.com/run/docs/triggering/https-request.md.txt#url).

2. Verify that the task was executed successfully by reading the logs.

   ```bash
   gcloud logging read 'resource.type="cloud_tasks_queue" resource.labels.queue_id="QUEUE_NAME" severity>=DEFAULT' --limit=3
   ```

   The logs should look similar to the following:

       jsonPayload:
       '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog
       task: projects/PROJECT_ID/locations/REGION/queues/QUEUE_NAME/tasks/TASK_ID
       taskCreationLog:
          scheduleTime: '2024-07-04T19:00:27.801837Z'
          status: OK
          targetAddress: GET https://www.example.com/
          targetType: HTTP
       ...

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, delete the Google Cloud project with the
resources.

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

Delete a Google Cloud project:

```
gcloud projects delete PROJECT_ID
```

Alternatively, you can delete the Cloud Tasks queue:

```bash
gcloud tasks queues delete QUEUE_NAME \
    --location=REGION
```

## What's next

- [Manage queues and tasks](https://docs.cloud.google.com/tasks/docs/deleting-appengine-queues-and-tasks)
- [Create Cloud Tasks queues](https://docs.cloud.google.com/tasks/docs/creating-queues.md.txt)
- [RPC API reference](https://docs.cloud.google.com/tasks/docs/reference/rpc.md.txt)
- [REST API reference](https://docs.cloud.google.com/tasks/docs/reference/rest.md.txt)