1
votes

Hi I have been trying to import a dataset using ckan api call via python's urllib2 following the documentation at http://docs.ckan.org/en/latest/api/ the Code I am running is `

#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint

dataset_dict = {
        'name': 'my_dataset_name5',
        'notes': 'A long description of my dataset',
}

data_string = urllib.quote(json.dumps(dataset_dict))

request = urllib2.Request(
        'http://<ckan server ip>/api/action/package_create')

request.add_header('Authorization', 'my api key')

response = urllib2.urlopen(request, data_string)
assert response.code == 200

response_dict = json.loads(response.read())
assert response_dict['success'] is True

created_package = response_dict['result']
pprint.pprint(created_package)`

However it gives the following error:

Traceback (most recent call last):File "autodatv2.py", line 26, in response = urllib2.urlopen(request, data_string)File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout)File "/usr/lib64/python2.7/urllib2.py", line 437, in open response = meth(req, response)File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib64/python2.7/urllib2.py", line 475, in error return self._call_chain(*args) File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 409: Conflict

I am running CKAN version 2.4 with python 2.7.10 on a Amazon ec2 instance and echo $HTTP_PROXY shows nothing so I'm assuming it not a proxy issue.. Could someone please provide any help to resolve this issue

1

1 Answers

4
votes

CKAN is returning HTTP error 409, which could mean nearly anything. e.g You could have a missing field or there may be already a dataset of that name in the CKAN.

There will be an error message explaining the problem in the response body and also on in the CKAN log.

Frankly, using urllib2 is making life hard for yourself. To talk to the CKAN API in python, at the very least use 'requests', but best practice is to use https://github.com/ckan/ckanapi e.g.

import ckanapi

demo = ckanapi.RemoteCKAN('http://demo.ckan.org',
    apikey='phony-key',
    user_agent='ckanapiexample/1.0 (+http://example.com/my/website)')

pkg = demo.action.package_create(name='my-dataset', title='not going to work')