Last active
August 23, 2016 17:30
-
-
Save mosampaio/d385ad31a2c93a62b0f0f026c7d88b45 to your computer and use it in GitHub Desktop.
How to consume a json Rest API with Python using tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remember to add the dependency > pip install unirest | |
from tornado.httpclient import HTTPClient | |
from tornado.httpclient import AsyncHTTPClient | |
import tornado.ioloop | |
url = 'https://api.github.com/repos/twilio/twilio-node/contributors' | |
headers = {'user-agent': 'my app'} | |
# Synchronous | |
http_client = HTTPClient() | |
response = http_client.fetch(url, headers=headers) | |
print(response.body) | |
http_client.close() | |
# Asynchronous | |
async_http_client = AsyncHTTPClient() | |
async_http_client.fetch(url, headers=headers, callback=lambda r: print(r.body)) | |
# Start the tornado ioloop | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment