Skip to content

Instantly share code, notes, and snippets.

@richardhsu

richardhsu/send_post.py

Last active Dec 18, 2015
Embed
What would you like to do?
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