231

I need to upload some data to a server using HTTP PUT method in Python. From my brief reading of the urllib2 docs, it only does HTTP POST.

Is there any way to do an HTTP PUT in Python?

0

14 Answers 14

330

I've used a variety of python HTTP libs in the past, and I've settled on requests as my favourite. Existing libs had pretty useable interfaces, but code can end up being a few lines too long for simple operations. A basic PUT in requests looks like:

payload = {'username': 'bob', 'email': '[email protected]'}
>>> r = requests.put("http://somedomain.org/endpoint", data=payload)

You can then check the response status code with:

r.status_code

or the response with:

r.content

Requests has a lot synactic sugar and shortcuts that'll make your life easier.

10
  • 11
    Even though the above code looks extremely simple, don't infer that 'requests' is in any way lacking or under-powered. It is extremely capable, just with a highly tidy interface. Commented Feb 8, 2012 at 18:17
  • 5
    I wonder how long it'll take this answer to gradually accumulate votes until it becomes the new highest-voted answer? Commented Feb 9, 2012 at 12:27
  • 2
    You don't understand how much this is awesome!!! I was struggling with a crummy java library! ... I think I kinda love you for pointing to "Requests"!
    – Shehaaz
    Commented Apr 26, 2013 at 5:13
  • 4
    Use json=payload parameter if you want the data to be in the body. Commented Mar 31, 2017 at 21:06
  • 1
    I want to quickly point out that this interface is better than the other answer, because it's functional programming. Why create an object when a function with simple data structures as parameters will do. I wish other python libraries would follow suit.
    – Marc
    Commented Nov 1, 2018 at 18:31
246
import urllib2
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request('http://example.org', data='your_put_data')
request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
url = opener.open(request)
10
  • 4
    Looks like a bit of a dirty hack, but it seems to work elegantly and completly Commented Sep 22, 2008 at 9:14
  • 6
    It would be less of a hack if you were to subclass urllib2.Request instead of monkey-patching it. Commented Oct 18, 2011 at 11:59
  • 23
    This answer was brilliant when it was written, but nowadays it's a lot easier to use the 'requests' package instead, see John Carter's answer. 'Requests' is in no way a toy - it is extremely capable. Commented Feb 8, 2012 at 18:15
  • 5
    This answer is canonical, but outdated. Please, consider using requests library instead.
    – jsalonen
    Commented Mar 25, 2012 at 17:11
  • 13
    Thank you, I didn't want to use anything outside of the standard python library and this works perfect. I don't quite understand why the urllib2 is designed to only cater for GET and POST as standard, but this work around is champion. Can the urllib2.build_opener(urllib2.HTTPHandler) be reused in multiple calls? Commented Nov 16, 2012 at 1:12
46

Httplib seems like a cleaner choice.

import httplib
connection =  httplib.HTTPConnection('1.2.3.4:1234')
body_content = 'BODY CONTENT GOES HERE'
connection.request('PUT', '/url/path/to/put/to', body_content)
result = connection.getresponse()
# Now result.status and result.reason contains interesting stuff
5
  • Don't use httplib if you (or any of your potential users) need proxy support. See this article for details. Commented Oct 18, 2011 at 11:55
  • Why would you say that? Your article clearly states that it works. See Rolf Wester's answer, he says that urllib fails but httplib works.
    – Spooles
    Commented Oct 20, 2011 at 14:41
  • httplib only works when the user explicitly connects to the proxy and modifies the request to include the full URL in the GET parameter. The reason urllib failed was because the http_proxy wasn't set properly. urllib uses httplib under the scenes, but also handles redirects, proxies, etc. Commented Oct 21, 2011 at 14:54
  • While this solution would work, the one using requests is much simpler, elegant, and in my opinion better.
    – tgrosinger
    Commented Jun 29, 2012 at 20:21
  • 1
    @tgrosinger I agree. This answer was posted before requests existed. As of now, the only case where this solution would be better is if only Python Standard Library would be available. Otherwise, I'd also recommend to use requests.
    – Spooles
    Commented Aug 22, 2012 at 13:59
14

You can use the requests library, it simplifies things a lot in comparison to taking the urllib2 approach. First install it from pip:

pip install requests

More on installing requests.

Then setup the put request:

import requests
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}

# Create your header as required
headers = {"content-type": "application/json", "Authorization": "<auth-key>" }

r = requests.put(url, data=json.dumps(payload), headers=headers)

See the quickstart for requests library. I think this is a lot simpler than urllib2 but does require this additional package to be installed and imported.

3
  • Doesnt requests support PUT as well?
    – Jeef
    Commented Sep 28, 2014 at 16:48
  • requests supports get, put, post, delete head and options. Fixed the example to use put. Check out requests quickstart.
    – radtek
    Commented Sep 29, 2014 at 13:22
  • @RPradeep Thanks for that.
    – radtek
    Commented Feb 22, 2016 at 20:23
10

This was made better in python3 and documented in the stdlib documentation

The urllib.request.Request class gained a method=... parameter in python3.

Some sample usage:

req = urllib.request.Request('https://example.com/', data=b'DATA!', method='PUT')
urllib.request.urlopen(req)
8

You should have a look at the httplib module. It should let you make whatever sort of HTTP request you want.

1
  • Nice solution, quiet pythonic but a bit too close to the metal and involving writing a lot of other code already Commented Sep 22, 2008 at 9:11
8

I needed to solve this problem too a while back so that I could act as a client for a RESTful API. I settled on httplib2 because it allowed me to send PUT and DELETE in addition to GET and POST. Httplib2 is not part of the standard library but you can easily get it from the cheese shop.

1
  • 2
    httplib2 is borderline abandonware. It has a long list of bugs that go unfixed despite community contributions (patches). I suggest thinking twice before using httplib2 in any production environments. Commented Oct 18, 2011 at 11:58
6

I also recommend httplib2 by Joe Gregario. I use this regularly instead of httplib in the standard lib.

3

Have you taken a look at put.py? I've used it in the past. You can also just hack up your own request with urllib.

4
  • I don't really wanna use some random guys http library Commented Sep 22, 2008 at 9:10
  • trying import put getting # pip install put Downloading/unpacking put Could not find any downloads that satisfy the requirement put Cleaning up... No distributions at all found for put Commented Aug 18, 2017 at 7:58
  • # tail -f /root/.pip/pip.log Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 278, in run requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle) File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1178, in prepare_files Commented Aug 18, 2017 at 8:00
  • url = finder.find_requirement(req_to_install, upgrade=self.upgrade) File "/usr/lib/python2.7/dist-packages/pip/index.py", line 277, in find_requirement raise DistributionNotFound('No distributions at all found for %s' % req) DistributionNotFound: No distributions at all found for put Commented Aug 18, 2017 at 8:00
2

You can of course roll your own with the existing standard libraries at any level from sockets up to tweaking urllib.

http://pycurl.sourceforge.net/

"PyCurl is a Python interface to libcurl."

"libcurl is a free and easy-to-use client-side URL transfer library, ... supports ... HTTP PUT"

"The main drawback with PycURL is that it is a relative thin layer over libcurl without any of those nice Pythonic class hierarchies. This means it has a somewhat steep learning curve unless you are already familiar with libcurl's C API. "

1
  • I'm sure it would work, but I want something a bit more pythonic Commented Sep 22, 2008 at 9:13
2

If you want to stay within the standard library, you can subclass urllib2.Request:

import urllib2

class RequestWithMethod(urllib2.Request):
    def __init__(self, *args, **kwargs):
        self._method = kwargs.pop('method', None)
        urllib2.Request.__init__(self, *args, **kwargs)

    def get_method(self):
        return self._method if self._method else super(RequestWithMethod, self).get_method()


def put_request(url, data):
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = RequestWithMethod(url, method='PUT', data=data)
    return opener.open(request)
2

You can use requests.request

import requests

url = "https://www.example/com/some/url/"
payload="{\"param1\": 1, \"param1\": 2}"
headers = {
  'Authorization': '....',
  'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)

print(response.text)
0

A more proper way of doing this with requests would be:

import requests

payload = {'username': 'bob', 'email': '[email protected]'}

try:
    response = requests.put(url="http://somedomain.org/endpoint", data=payload)
    response.raise_for_status()
except requests.exceptions.RequestException as e:
    print(e)
    raise

This raises an exception if there is an error in the HTTP PUT request.

0

Using urllib3

To do that, you will need to manually encode query parameters in the URL.

>>> import urllib3
>>> http = urllib3.PoolManager()
>>> from urllib.parse import urlencode
>>> encoded_args = urlencode({"name":"Zion","salary":"1123","age":"23"})
>>> url = 'http://dummy.restapiexample.com/api/v1/update/15410' + encoded_args
>>> r = http.request('PUT', url)
>>> import json
>>> json.loads(r.data.decode('utf-8'))
{'status': 'success', 'data': [], 'message': 'Successfully! Record has been updated.'}

Using requests

>>> import requests
>>> r = requests.put('https://httpbin.org/put', data = {'key':'value'})
>>> r.status_code
200

Not the answer you're looking for? Browse other questions tagged or ask your own question.