wild276
wild276

Reputation: 43

Creating a package with resources using the package_create API call in Python

I'm trying to upload a data package with resources to CKAN (via Python). I can upload the package successfully without the "resources", but I keep hitting this error with them: 'message': "Only lists of dicts can be placed against subschema ('resources',), not <type 'list'>"

I've tried reformatting the Python dictionary several times and also used json.dumps() on the dictionary, but I then hit a json error when calling the API.

test_dict = 
      {
      'title': 'title of my dataset',
      'start': '2018-09-15 00:00:00',
      'end': '2018-09-20 00:00:00',
      'fact': 'interesting fact',
      'ReportNo': 1234,
      'type': 'data',
      'notes': ' ',
      'owner_org': 'Org',
      'maintainer': 'Me',
      'name': 'Test package for S3',
      'resources': [
          {
                    'package_id': '',
                    'url': 'https://s3-test-bucket/test.txt',
                    'name': 'S3 URL testing',
                    'description': 'does description work?'
                    }
      ]
}

response = requests.post(url, test_dict, headers=auth)
response.json()

Expecting: 'success':'True'

Getting: 'message': "Only lists of dicts can be placed against subschema ('resources',), not ", '__type': 'Integrity Error'

Please can someone explain the correct Python dictionary format? Preferably with examples.

Thanks!

Upvotes: 1

Views: 723

Answers (1)

wild276
wild276

Reputation: 43

I found the answer based on this post: Create CKAN dataset using CKAN API and Python Requests library

Here's the code I'm using:

    headers = json.loads(open('API_KEY','rb').read())
    headers['Content-Type'] = 'application/x-www-form-urlencoded'
    ckan = 'https://yourckanwebsite/api/3/action/'
    uri = ckan + 'package_create'

    data_dict = urllib.parse.quote(json.dumps(test_dict))
    response = requests.post(uri, data=data_dict, headers=headers)
    response.json()

Upvotes: 1

Related Questions