Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 17:17
Show Gist options
  • Select an option

  • Save Averroes/539d2550a48f56e4b166 to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/539d2550a48f56e4b166 to your computer and use it in GitHub Desktop.
interacting with http services as a client
# 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