From b992fd995487ce80f52c9a2494b83b91aee66ce5 Mon Sep 17 00:00:00 2001 From: "Joab Leite S. Neto" Date: Mon, 10 Aug 2020 23:58:15 -0300 Subject: [PATCH 1/2] tasks: added json content-type request - fix imports and code block used on https://cloud.google.com/tasks/docs/creating-http-target-tasks#python --- tasks/create_http_task.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tasks/create_http_task.py b/tasks/create_http_task.py index 2d86549a533..76f8fe09e44 100644 --- a/tasks/create_http_task.py +++ b/tasks/create_http_task.py @@ -15,7 +15,6 @@ from __future__ import print_function import argparse -import datetime def create_http_task(project, @@ -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() @@ -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) @@ -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() @@ -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__': From a76f57c7d6f5cd5706902a1853d732212842e0f9 Mon Sep 17 00:00:00 2001 From: "Joab Leite S. Neto" Date: Tue, 11 Aug 2020 14:14:41 -0300 Subject: [PATCH 2/2] tasks: lint by rules E231 W293 --- tasks/create_http_task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/create_http_task.py b/tasks/create_http_task.py index 76f8fe09e44..23896209a10 100644 --- a/tasks/create_http_task.py +++ b/tasks/create_http_task.py @@ -57,8 +57,8 @@ def create_http_task(project, # Convert dict to JSON string payload = json.dumps(payload) # specify http content-type to application/json - task['http_request']['headers'] = {'Content-type':'application/json'} - + task['http_request']['headers'] = {'Content-type': 'application/json'} + # The API expects a payload of type bytes. converted_payload = payload.encode()