public
Created

How to use CkanClient with CKAN 1.8 for file upload using ckan_client.upload_file(). The fixed issue was about handling HTTP Redirects, the original version returns an "obscure" «('', 'Found')» "warning" instead. The problem was first reported here: http://lists.okfn.org/pipermail/ckan-dev/2012-August/002816.html

  • Download Gist
ckanclient_fix.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
def _post_multipart(self, selector, fields, files):
'''Post fields and files to an http host as multipart/form-data.
 
:param fields: a sequence of (name, value) tuples for regular form
fields
:param files: a sequence of (name, filename, value) tuples for data to
be uploaded as files
 
:returns: the server's response page
 
'''
 
from urlparse import urljoin, urlparse
 
content_type, body = self._encode_multipart_formdata(fields, files)
 
headers = self._auth_headers()
url = urljoin(self.base_location + urlparse(self.base_location).netloc, selector)
req = requests.post(url, data=dict(fields), files={files[0][0]: files[0][1:]}, headers=headers)
return req.status_code, req.error, req.headers, req.text
 
 
import ckanclient
 
# FIXME: no monkey patching here
ckanclient.CkanClient._post_multipart = _post_multipart

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.