MuffinTheMan
MuffinTheMan

Reputation: 1581

How to retrieve PAYLOAD for a Google Cloud Task via API

Is it possible to retrieve the PAYLOAD (see image below of what I can view in GCP's Cloud Tasks UI) for a GCP Cloud Task via Google's API? If so, how? Here's the Task documentation I've been looking through (I can't see anything that would get me the PAYLOAD): https://cloud.google.com/java/docs/reference/google-cloud-tasks/latest/com.google.cloud.tasks.v2.Task

GCP Cloud Task PAYLOAD

I also ran gcloud tasks describe [TASK] --response-view=full and couldn't see the PAYLOAD anywhere in the response.

This seems like obvious functionality, so I'm hoping I'm missing something (different API, perhaps). Thanks!

Upvotes: 1

Views: 1636

Answers (2)

Banty
Banty

Reputation: 971

You can achieve this using a combination of list_tasks and get_task methods from the documentation. Below is a python example:

from google.cloud import tasks_v2
import json

# create a client
client = tasks_v2.CloudTasksClient()

queue_path = client.queue_path('<project_id>', '<location>', '<queue_id>')

# list the tasks in the queue
request = tasks.ListTasksRequest(parent=queue_path)
response = client.list_tasks(request=request)

# get task details
for task in response:

    request_task_info = tasks_v2.GetTaskRequest(name=task.name, response_view='FULL')
    task_info = client.get_task(request=request_task_info)

    # get payload from details
    payload = json.loads(task_info.http_request.body)
    print(payload)

Also see the list and get HTTP request methods.

Upvotes: 1

DazWilkin
DazWilkin

Reputation: 40326

I suspect (!?) this is excluded from Task on retrieval perhaps for security reasons but, it appears not possible to get the request body from tasks in the queue.

Note: On way to investigate this is to use e.g. Chrome's Developer Tools to understand how Console achieves this.

You can validate this using gcloud and APIs Explorer:

 gcloud tasks list \
--queue=${QUEUE} \
--location=${LOCATION} \
--project=${PROJECT} \
 --format=yaml \
--log-http

Note: You don't need the --format to use --log-http but I was using both.

Or: projects.locations.queues.tasks.list and filling in the blanks (you don't need to include API key).

Even though all should be Task in my case (using HttpRequest, the response does not include body.

IIRC, when I used Cloud Tasks a while ago, I was similarly perplexed at being unable to access response bodies.

Upvotes: 0

Related Questions