1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| In [1]: import requests
In [3]: res = requests.post('http://localhost:8000/api-auth-token/', {'username' ...: :'tester', 'password': 'rhdiddl486'})
In [4]: res = requests.post('http://localhost:8000/api-token-auth/', {'username' ...: :'tester', 'password': 'rhdiddl486'})
In [5]: res Out[5]: <Response [200]>
In [6]: res.text Out[6]: '{"token":"d7ec115d8370de00f06c50ec5b61bd519b1038ae"}'
In [7]: res.json() Out[7]: {'token': 'd7ec115d8370de00f06c50ec5b61bd519b1038ae'}
In [8]: res.json()['token'] Out[8]: 'd7ec115d8370de00f06c50ec5b61bd519b1038ae'
In [9]: token = res.json()['token']
In [10]: headers = { ...: 'Authorization': 'Token ' + token, ...: }
In [11]: headers Out[11]: {'Authorization': 'Token d7ec115d8370de00f06c50ec5b61bd519b1038ae'}
In [12]: res = requests.get('http://localhost:800/api/post/', headers=headers)
In [13]: res = requests.get('http://localhost:8000/api/post/', headers=headers)
In [14]: res Out[14]: <Response [200]>
In [15]: res.text Out[15]: '[{"id":1,"author_username":"tester","message":"Hello","photo":null},{"id":2,"author_username":"tester","message":"Hello","photo":null},{"id":3,"author_username":"tester","message":"Hello","photo":null},{"id":4,"author_username":"tester","message":"Hello","photo":null},{"id":5,"author_username":"tester","message":"Hello","photo":null},{"id":6,"author_username":"tester","message":"Hello","photo":null},{"id":7,"author_username":"tester","message":"Hello","photo":null},{"id":8,"author_username":"tester","message":"Hello","photo":null},{"id":9,"author_username":"tester","message":"Hello","photo":null},{"id":10,"author_username":"tester","message":"Hello","photo":null},{"id":11,"author_username":"tester","message":"Hello","photo":null},{"id":12,"author_username":"tester","message":"Hello","photo":"http://localhost:8000/api/post/a_7Y7nc8e.jpg"}]'
In [16]: res.json() Out[16]: [{'id': 1, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 2, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 3, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 4, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 5, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 6, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 7, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 8, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 9, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 10, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 11, 'author_username': 'tester', 'message': 'Hello', 'photo': None}, {'id': 12, 'author_username': 'tester', 'message': 'Hello', 'photo': 'http://localhost:8000/api/post/a_7Y7nc8e.jpg'}]
In [17]: for post in res.json(): ...: print(post['message']) ...:
In [17]: for post in res.json(): ...: print(post['message']) ...: Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
In [19]: for post in res.json(): ...: print('{id}: {message}'.format(**post)) ...: ...: ...: 1: Hello 2: Hello 3: Hello 4: Hello 5: Hello 6: Hello 7: Hello 8: Hello 9: Hello 10: Hello 11: Hello 12: Hello
|