Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit bc21141

Browse files
authored
tasks: added json content-type request [(#4473)](GoogleCloudPlatform/python-docs-samples#4473)
- added json payload compatibility - fix imports and code block used on https://cloud.google.com/tasks/docs/creating-http-target-tasks#python ## Description Fixes #<ISSUE-NUMBER> Note: It's a good idea to open an issue first for discussion. ## Checklist - [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] Please **merge** this PR for me once it is approved. - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample
1 parent 05ce0c5 commit bc21141

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

samples/snippets/create_http_task.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from __future__ import print_function
1616

1717
import argparse
18-
import datetime
1918

2019

2120
def create_http_task(project,
@@ -30,6 +29,8 @@ def create_http_task(project,
3029

3130
from google.cloud import tasks_v2
3231
from google.protobuf import timestamp_pb2
32+
import datetime
33+
import json
3334

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

4445
# Construct the fully qualified queue name.
4546
parent = client.queue_path(project, location, queue)
@@ -52,6 +53,12 @@ def create_http_task(project,
5253
}
5354
}
5455
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+
5562
# The API expects a payload of type bytes.
5663
converted_payload = payload.encode()
5764

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

7986
print('Created task {}'.format(response.name))
87+
# [END cloud_tasks_create_http_task]
8088
return response
81-
# [END cloud_tasks_create_http_task]
8289

8390

8491
if __name__ == '__main__':

0 commit comments

Comments
 (0)