Created
April 10, 2015 17:17
-
-
Save Averroes/539d2550a48f56e4b166 to your computer and use it in GitHub Desktop.
interacting with http services as a client
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
| # A basic POST request | |
| from urllib import request, parse | |
| # Base URL being accessed | |
| url = 'http://httpbin.org/post' | |
| # Dictionary of query parameters (if any) | |
| parms = { | |
| 'name1' : 'value1', | |
| 'name2' : 'value2' | |
| } | |
| # Encode the query string | |
| querystring = parse.urlencode(parms) | |
| # Make a POST request and read the response | |
| u = request.urlopen(url, querystring.encode('ascii')) | |
| resp = u.read() | |
| import json | |
| from pprint import pprint | |
| json_resp = json.loads(resp.decode('utf-8')) | |
| pprint(json_resp) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment