在 Python 中使用 package_create API 调用创建包含资源的包
Creating a package with resources using the package_create API call in Python
我正在尝试将包含资源的数据包上传到 CKAN(通过 Python)。我可以在没有 "resources" 的情况下成功上传包,但我一直遇到这个错误:
'message': "Only lists of dicts can be placed against subschema ('resources',), not <type 'list'>"
我已经多次尝试重新格式化 Python 字典,并且还在字典上使用了 json.dumps(),但是我在调用 [=38] 时遇到了 json 错误=].
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()
预计:'success':'True'
得到:'message':"Only lists of dicts can be placed against subschema ('resources',), not ",
'__type': 'Integrity Error'
有人可以解释正确的 Python 词典格式吗?最好有例子。
谢谢!
我根据这个post找到了答案:Create CKAN dataset using CKAN API and Python Requests library
这是我使用的代码:
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()
我正在尝试将包含资源的数据包上传到 CKAN(通过 Python)。我可以在没有 "resources" 的情况下成功上传包,但我一直遇到这个错误:
'message': "Only lists of dicts can be placed against subschema ('resources',), not <type 'list'>"
我已经多次尝试重新格式化 Python 字典,并且还在字典上使用了 json.dumps(),但是我在调用 [=38] 时遇到了 json 错误=].
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()
预计:'success':'True'
得到:'message':"Only lists of dicts can be placed against subschema ('resources',), not ", '__type': 'Integrity Error'
有人可以解释正确的 Python 词典格式吗?最好有例子。
谢谢!
我根据这个post找到了答案:Create CKAN dataset using CKAN API and Python Requests library
这是我使用的代码:
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()