Forked from Integralist/Tornado AsyncHTTPClient POST JSON example.py
Created
January 21, 2019 09:11
-
-
Save cundi/f06479c4f598bda92d56de0e701d23ec to your computer and use it in GitHub Desktop.
[Tornado AsyncHTTPClient POST form params example] #python #tornado #post #httpclient #asynchttpclient
This file contains hidden or 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
import urllib | |
def handle_request(http_response): | |
# do something with HTTPResponse object | |
post_data = { 'data': 'test data' } | |
body = urllib.parse.urlencode(post_data) | |
http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, body=body) | |
# handle_request callback can be omitted (and in fact is deprecated since Tornado version 5.1) | |
response = await http_client.fetch("http://0.0.0.0:8888", method='POST', headers=None, body=body) |
This file contains hidden or 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
# synchronous api | |
# response = sg.client.mail.send.post(request_body=data) | |
# except urllib.error.HTTPError as exc: | |
# async implementation | |
api_endpoint = 'https://api.sendgrid.com/v3/mail/send' | |
headers = {'Authorization': f'Bearer {sendgrid_api_key}', | |
'Content-Type': 'application/json'} | |
json_data = json.dumps(data) | |
response = await http_client.fetch(api_endpoint, | |
raise_error=False, | |
method='POST', | |
body=json_data, | |
headers=headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment