Skip to content

Instantly share code, notes, and snippets.

@mammadori
Created February 13, 2013 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mammadori/4945812 to your computer and use it in GitHub Desktop.
Save mammadori/4945812 to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment