Skip to content

Instantly share code, notes, and snippets.

@richardhsu
Last active December 18, 2015 22:29
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 richardhsu/5854844 to your computer and use it in GitHub Desktop.
Save richardhsu/5854844 to your computer and use it in GitHub Desktop.
Submitting a JSON post request in Python with an `application/json` type.
#!/usr/bin/env python
import json
import urllib, urllib2
# URL and Data to Send
url = 'http://localhost/api/table_name'
values = { 'user': 'username',
'count': 5,
'status': False }
# Construction JSON data and send request
data = json.dumps(values)
request = urllib2.Request(url, data, {'Content-Type': 'application/json'})
# Send the request
try:
response = urllib2.urlopen(request)
# Print the response of the request
response_data = response.read()
print response.code
print response.headers
print response_data
except urllib2.HTTPError as e:
print "Error: " + str(e.code)
print "Message: " + e.msg
print "Content:"
print e.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment