{"id":274,"date":"2008-10-21T15:00:44","date_gmt":"2008-10-21T19:00:44","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/?p=274"},"modified":"2008-10-21T14:22:53","modified_gmt":"2008-10-21T18:22:53","slug":"putting-and-deleteing-in-python-urllib2","status":"publish","type":"post","link":"http:\/\/benjamin.smedbergs.us\/blog\/2008-10-21\/putting-and-deleteing-in-python-urllib2\/","title":{"rendered":"PUTting and DELETEing in python urllib2"},"content":{"rendered":"<p>The urllib2 Python module makes it pretty simple to GET and POST data using HTTP (and other protocols). But there isn&#8217;t a good built-in way to issue HTTP PUT or DELETE requests. I ran into this limitation while working on a <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=460999\">project<\/a> to upload automatically generated documentation to the Mozilla Developer Center. The DekiWiki API for uploading an file attachment uses the HTTP PUT method.<\/p>\n<p>It turns out there is an easy workaround. You can subclass the urllib2.Request class and explicitly override the method:<\/p>\n<pre class=\"code\">import urllib2\r\n\r\nclass RequestWithMethod(urllib2.Request):\r\n  def __init__(self, method, *args, **kwargs):\r\n    self._method = method\r\n    urllib2.Request.__init__(*args, **kwargs)\r\n\r\n  def get_method(self):\r\n    return self._method<\/pre>\n<p>Preview for Thursday&#8217;s post: the generated documentation is <a href=\"http:\/\/developer.mozilla.org\/en\/nsAString_internal\">already online<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The urllib2 Python module makes it pretty simple to GET and POST data using HTTP (and other protocols). But there isn&#8217;t a good built-in way to issue HTTP PUT or DELETE requests. I ran into this limitation while working on a project to upload automatically generated documentation to the Mozilla Developer Center. The DekiWiki API [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[34,169,32,168],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-mozilla","category-untagged","tag-documentation","tag-http","tag-python","tag-urllib2"],"_links":{"self":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":1,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":275,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions\/275"}],"wp:attachment":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}