[ckan-dev] Problem making HTTP request to create a package using CKAN API

David davisoski at gmail.com
Tue Dec 20 18:30:40 UTC 2016


Hi.

I'm trying to create a package test in *demo.ckan.org* using api with 
the python example in:

http://docs.ckan.org/en/latest/api/index.html#example-importing-datasets-with-the-ckan-api


This is the code taken from the web example, just changed API KEY

# Put the details of the dataset we're going to create into a dict.
dataset_dict = {
     'name': 'my_dataset_name',
     'notes': 'A long description of my dataset',
}

# Use the json module to dump the dictionary to a string for posting.
data_string = urllib.quote(json.dumps(dataset_dict))

# We'll use the package_create function to create a new dataset.
request = urllib2.Request(
     'http://demo.ckan.org/api/3/action/package_create')

# Creating a dataset requires an authorization header.
# Replace *** with your API key, from your user account on the CKAN site
# that you're creating the dataset on.
request.add_header('Authorization', '97caad21-8632-4372-98fe-a24cdcaa90dc')

# Make the HTTP request.
response = urllib2.urlopen(request, data_string)
assert response.code == 200

# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.read())
assert response_dict['success'] is True

# package_create returns the created package as its result.
created_package = response_dict['result']
pprint.pprint(created_package)

I'm getting error:

Traceback (most recent call last):
   File "C:/create_dataset_ckan_api_demo_org.py", line 40, in <module>
     response = urllib2.urlopen(request, data_string)
   File "C:\Python27\lib\urllib2.py", line 154, in urlopen
     return opener.open(url, data, timeout)
   File "C:\Python27\lib\urllib2.py", line 437, in open
     response = meth(req, response)
   File "C:\Python27\lib\urllib2.py", line 550, in http_response
     'http', request, response, code, msg, hdrs)
   File "C:\Python27\lib\urllib2.py", line 475, in error
     return self._call_chain(*args)
   File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
     result = func(*args)
   File "C:\Python27\lib\urllib2.py", line 558, in*http_error_default**
**    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)*
HTTPError: HTTP Error 409: Conflict


BUT

the code to get activities from a user works great, so I suppose is not 
a key problem


request = 
urllib2.Request('http://demo.ckan.org/api/3/action/dashboard_activity_list')
request.add_header('Authorization', '97caad21-8632-4372-98fe-a24cdcaa90dc')
response_dict = json.loads(urllib2.urlopen(request, '{}').read())


assert response_dict['success'] is True
result = response_dict['result']
pprint.pprint(result)


Any help?

Thanks



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20161220/b11052f8/attachment-0002.html>


More information about the ckan-dev mailing list