blob: 534ac9ed03ceedd30f40d878cc2c92c5785282be [file] [log] [blame]
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +01001# 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
6from __future__ import (
7 absolute_import,
8 print_function,
9 unicode_literals,
10 )
11
12str = None
13
14__metaclass__ = type
15__all__ = [
16 'encode_json_data',
17 ]
18
19import json
20
21
22def 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