Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 1 | # Copyright 2012 Canonical Ltd. This software is licensed under the |
| 2 | # GNU Affero General Public License version 3 (see the file LICENSE). |
| 3 | |
| 4 | """Encoding requests as JSON data.""" |
| 5 | |
| 6 | from __future__ import ( |
| 7 | absolute_import, |
| 8 | print_function, |
| 9 | unicode_literals, |
| 10 | ) |
| 11 | |
| 12 | str = None |
| 13 | |
| 14 | __metaclass__ = type |
| 15 | __all__ = [ |
| 16 | 'encode_json_data', |
| 17 | ] |
| 18 | |
| 19 | import json |
| 20 | |
| 21 | |
| 22 | def encode_json_data(params): |
| 23 | """Encode params as JSON and set up headers for an HTTP POST. |
| 24 | |
| 25 | :param params: Can be a dict or a list, but should generally be a dict, to |
| 26 | match the key-value data expected by most receiving APIs. |
| 27 | :return: (body, headers) |
| 28 | """ |
| 29 | body = json.dumps(params) |
| 30 | headers = { |
| 31 | 'Content-Length': unicode(len(body)), |
| 32 | 'Content-Type': 'application/json', |
| 33 | } |
| 34 | return body, headers |