I am using CKAN version 2.2 and trying to automate the creation of a dataset and resource loading. It seems I cannot create a dataset using the python query library. I get a 400 error code. The code:
import requests, json
dataset_dict = {
'name': 'testdataset',
'notes': 'A long description of my dataset',
}
d_url = 'https://mywebsite.ca/api/action/package_create'
auth = {'Authorization': 'myKeyHere'}
f = [('upload', file('PathToMyFile'))]
r = requests.post(d_url, data=dataset_dict, headers=auth)
Oddly, I can create a new resource and upload the file using the python query library. The code is based on this documentation. The code:
import requests, json
res_dict = {
'package_id':'testpackage',
'name': 'testresource',
'description': 'A long description of my resource!',
'format':'CSV'
}
res_url = 'https://mywebsite.ca/api/action/resource_create'
auth = {'Authorization': 'myKey'}
f = [('upload', file('pathToMyFile'))]
r = requests.post(res_url, data=res_dict, headers=auth, files=f)
I can also create a dataset using the method in the CKAN documentation using the python built-in libraries. Documentation: CKAN 2.2
the code:
import urllib2
import urllib
import json
import pprint
dataset_dict = {
'name': 'test1',
'notes': 'A long description of my dataset',
}
data_string = urllib.quote(json.dumps(dataset_dict))
request = urllib2.Request('https://myserver.ca/api/action/package_create')
request.add_header('Authorization', 'myKey')
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)
, . package_create resource_create , , . CKAN. - ?
.