Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tasks/create_http_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from __future__ import print_function

import argparse
import datetime


def create_http_task(project,
Expand All @@ -30,6 +29,8 @@ def create_http_task(project,

from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2
import datetime
import json

# Create a client.
client = tasks_v2.CloudTasksClient()
Expand All @@ -39,7 +40,7 @@ def create_http_task(project,
# queue = 'my-queue'
# location = 'us-central1'
# url = 'https://example.com/task_handler'
# payload = 'hello'
# payload = 'hello' or {'param': 'value'} for application/json

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)
Expand All @@ -52,6 +53,12 @@ def create_http_task(project,
}
}
if payload is not None:
if isinstance(payload, dict):
# Convert dict to JSON string
payload = json.dumps(payload)
# specify http content-type to application/json
task['http_request']['headers'] = {'Content-type': 'application/json'}

# The API expects a payload of type bytes.
converted_payload = payload.encode()

Expand All @@ -77,8 +84,8 @@ def create_http_task(project,
response = client.create_task(parent, task)

print('Created task {}'.format(response.name))
# [END cloud_tasks_create_http_task]
return response
# [END cloud_tasks_create_http_task]


if __name__ == '__main__':
Expand Down