Last active
December 18, 2015 22:29
-
-
Save richardhsu/5854844 to your computer and use it in GitHub Desktop.
Submitting a JSON post request in Python with an `application/json` type.
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
#!/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