Skip to content

Instantly share code, notes, and snippets.

@psychok7
Last active December 17, 2015 06:49
Show Gist options
  • Save psychok7/5568291 to your computer and use it in GitHub Desktop.
Save psychok7/5568291 to your computer and use it in GitHub Desktop.
Python Post to REST API using Basic Authentication
import urllib2, json
def post():
def basic_authorization(user, password):
s = user + ":" + password
return "Basic " + s.encode("base64").rstrip()
data = json.dumps({'username':'david2', 'email':'david@gmail.com' , 'password':'mypassword'})
req = urllib2.Request("http://192.168.1.77:8080/api/stats/user/",
headers = {"Authorization": basic_authorization("y", "y"),"Content-Type": "application/json"},
data = data
)
#req.get_method = lambda: 'PUT'
f = urllib2.urlopen(req)
post()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment