From ca2e140c30e27143f11e8df6a51d8bb2ed0046e6 Mon Sep 17 00:00:00 2001 From: David Watson Date: Tue, 22 Dec 2015 01:21:22 -0500 Subject: [PATCH] Add example docs for python-requests-jwt --- docs/examples/python-requests-jwt.md | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 docs/examples/python-requests-jwt.md diff --git a/docs/examples/python-requests-jwt.md b/docs/examples/python-requests-jwt.md new file mode 100644 index 000000000..57a56555f --- /dev/null +++ b/docs/examples/python-requests-jwt.md @@ -0,0 +1,37 @@ +## Accessing PostgREST API using Python Requests and Requests-JWT + +The following HTTP client example should work on Python 3.x. If you're using python < 3.x you may need to change the print functions to statements or use: + + from __future__ import print_function + +This code relies on setting up the PostgreSQL auth functions and grants correctly first. Follow these instructions: + +http://postgrest.com/examples/users/ + +After completing the configuration, be sure to create a user with email, password, role, and verified flag. We'll use that user to login in the code below. + +Install the required libraries + + pip install requests + pip install Requests-jwt + +Then, from a python interpreter or script: + + import requests + import requests_jwt + import json + + # email and password auth through postgrest + resp = requests.post('http://localhost:3000/rpc/login', json={"email": "you@yours.com", "pass": "dog"}) + if resp.status_code != 200: + raise Exception() + + # JWT auth using the token above + token = json.loads(resp.text)['token'] + auth = requests_jwt.JWTAuth(token) + r = requests.get('http://localhost:3000/weight', auth=auth) + if r.status_code != 200: + raise Exception() + + for each in r.json(): + print(each)