Created
November 10, 2011 18:27
-
-
Save limed/1355667 to your computer and use it in GitHub Desktop.
Using PUT in urllib2
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/python | |
import urllib2 | |
import json | |
apikey = '' | |
user = '' | |
baseurl = '' | |
query_enable = { 'is_enabled' : True } | |
query_disable = { 'is_enabled' : False, 'disabled_reason' : "infringement3" } | |
url = baseurl + user + "?key=" + apikey | |
json_data = json.dumps(query_disable) | |
opener = urllib2.build_opener(urllib2.HTTPHandler) | |
request = urllib2.Request(url, data=json_data) | |
request.add_header('Content-Type', 'application/json') | |
request.get_method = lambda: 'PUT' | |
url = opener.open(request) | |
print url.read() |
Hello guys, could you please explain why we're using lamda here for get_method()?
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for that... Here it is with Basic Auth as well for to PUT JSON. The encodestring() variable may be outdated but your answer helped me on an old server with only Python 2.6 and no option to install requests.