From 4626b4480bc3f18d169d0435b983eb80aa8658b1 Mon Sep 17 00:00:00 2001 From: David Watson Date: Tue, 22 Dec 2015 12:55:54 -0500 Subject: [PATCH] Update doc with pagination using Range header --- docs/examples/python-requests-jwt.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/examples/python-requests-jwt.md b/docs/examples/python-requests-jwt.md index ab2cfdd98..a82ca0157 100644 --- a/docs/examples/python-requests-jwt.md +++ b/docs/examples/python-requests-jwt.md @@ -25,13 +25,18 @@ Then, from a python interpreter or script: # 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(): - # do as you wish with each row - print(each) + # Handle pagination using the Range header + for i in range(0, upper_bound, 20): + this_range = '{0}-{1}'.format(i, i+20 if i+20 < upper_bound else upper_bound) + headers = {"Range": "{}".format(this_range)} + r = requests.get('http://localhost:3000/posts', auth=auth, headers=headers) + if r.status_code != 200: + raise Exception() + page = r.json() + # put page into pagination control or the like The preceding 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: