Skip to content

Commit 23730de

Browse files
authored
Merge pull request #284 from twm/278-json-docs
Improve JSON documentation
2 parents 7a8f45a + 0ce26b2 commit 23730de

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

changelog.d/278.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
An example of sending and receiving JSON has been added.

docs/examples/basic_post.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import json
2-
31
from twisted.internet.task import react
42
from _utils import print_response
53

64
import treq
75

86

9-
def main(reactor, *args):
10-
d = treq.post('https://httpbin.org/post',
11-
json.dumps({"msg": "Hello!"}).encode('ascii'),
12-
headers={b'Content-Type': [b'application/json']})
7+
def main(reactor):
8+
d = treq.post("https://httpbin.org/post",
9+
data={"form": "data"})
1310
d.addCallback(print_response)
1411
return d
1512

docs/examples/json_post.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pprint import pprint
2+
3+
from twisted.internet import defer
4+
from twisted.internet.task import react
5+
6+
import treq
7+
8+
9+
@defer.inlineCallbacks
10+
def main(reactor):
11+
response = yield treq.post(
12+
'https://httpbin.org/post',
13+
json={"msg": "Hello!"},
14+
)
15+
data = yield response.json()
16+
pprint(data)
17+
18+
react(main, [])

docs/howto.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ or a ``list`` of ``str`` values.
5858

5959
Full example: :download:`query_params.py <examples/query_params.py>`
6060

61+
JSON
62+
----
63+
64+
:meth:`HTTPClient.request() <treq.client.HTTPClient.request>` supports a *json* keyword argument that gives a data structure to serialize as JSON (using :func:`json.dumps()`).
65+
This also implies a ``Content-Type: application/json`` request header.
66+
The *json* parameter is mutually-exclusive with *data*.
67+
68+
The :meth:`_Response.json()` method decodes a JSON response body.
69+
It buffers the whole response and decodes it with :func:`json.loads()`.
70+
71+
.. literalinclude:: examples/json_post.py
72+
:linenos:
73+
:pyobject: main
74+
75+
Full example: :download:`json_post.py <examples/json_post.py>`
76+
6177
Auth
6278
----
6379

docs/index.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ GET
2929
+++
3030

3131
.. literalinclude:: examples/basic_get.py
32-
:linenos:
33-
:lines: 7-10
32+
:pyobject: main
3433

3534
Full example: :download:`basic_get.py <examples/basic_get.py>`
3635

3736
POST
3837
++++
3938

4039
.. literalinclude:: examples/basic_post.py
41-
:linenos:
42-
:lines: 9-14
40+
:pyobject: main
4341

4442
Full example: :download:`basic_post.py <examples/basic_post.py>`
4543

0 commit comments

Comments
 (0)