Skip to content

Instantly share code, notes, and snippets.

@mosampaio
Last active August 23, 2016 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosampaio/d385ad31a2c93a62b0f0f026c7d88b45 to your computer and use it in GitHub Desktop.
Save mosampaio/d385ad31a2c93a62b0f0f026c7d88b45 to your computer and use it in GitHub Desktop.
How to consume a json Rest API with Python using tornado
# 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