15
15
from __future__ import print_function
16
16
17
17
import argparse
18
- import datetime
19
18
20
19
21
20
def create_http_task (project ,
@@ -30,6 +29,8 @@ def create_http_task(project,
30
29
31
30
from google .cloud import tasks_v2
32
31
from google .protobuf import timestamp_pb2
32
+ import datetime
33
+ import json
33
34
34
35
# Create a client.
35
36
client = tasks_v2 .CloudTasksClient ()
@@ -39,7 +40,7 @@ def create_http_task(project,
39
40
# queue = 'my-queue'
40
41
# location = 'us-central1'
41
42
# url = 'https://example.com/task_handler'
42
- # payload = 'hello'
43
+ # payload = 'hello' or {'param': 'value'} for application/json
43
44
44
45
# Construct the fully qualified queue name.
45
46
parent = client .queue_path (project , location , queue )
@@ -52,6 +53,12 @@ def create_http_task(project,
52
53
}
53
54
}
54
55
if payload is not None :
56
+ if isinstance (payload , dict ):
57
+ # Convert dict to JSON string
58
+ payload = json .dumps (payload )
59
+ # specify http content-type to application/json
60
+ task ['http_request' ]['headers' ] = {'Content-type' : 'application/json' }
61
+
55
62
# The API expects a payload of type bytes.
56
63
converted_payload = payload .encode ()
57
64
@@ -77,8 +84,8 @@ def create_http_task(project,
77
84
response = client .create_task (parent , task )
78
85
79
86
print ('Created task {}' .format (response .name ))
87
+ # [END cloud_tasks_create_http_task]
80
88
return response
81
- # [END cloud_tasks_create_http_task]
82
89
83
90
84
91
if __name__ == '__main__' :
0 commit comments