Created
February 13, 2013 16:27
-
-
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
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
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