我正在使用CKAN 2.2版,并试图自动创建数据集和上传资源。我似乎无法使用python requests库创建数据集。我收到400错误代码。代码:
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)
很奇怪,我am能够创建新资源并使用python requests库上传文件。该代码基于this documentation.代码:
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)
我还可以使用内置的python库,使用CKAN文档中的方法创建数据集。文档:CKAN 2.2
代码:
#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint
# Put the details of the dataset we're going to create into a dict.
dataset_dict = {
'name': 'test1',
'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('https://myserver.ca/api/action/package_create')
# Creating a dataset requires an authorization header.
request.add_header('Authorization', 'myKey')
# 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)
我不确定我为什么创建数据集的方法不起作用。 package_create和resource_create函数的文档非常相似,我希望能够使用相同的技术。我希望在与CKAN的所有交易中都使用请求包。是否有人能够成功使用请求库创建数据集?
非常感谢您的帮助。
我终于回到这个问题,并弄清楚了。爱丽丝检查编码的建议非常接近。尽管requests会为您进行编码,但它也会根据输入自行决定哪种编码类型合适。如果文件和JSON字典一起传入,则requests会自动进行CKAN接受的多部分/表单数据编码,因此请求成功。
但是,如果我们通过only JSON字典,则默认编码为form编码。 CKAN需要不带文件的请求进行URL编码(application / x-www-form-urlencoded)。为了防止requests进行任何编码,我们可以将我们的参数作为字符串传递,然后requests将仅执行POST。这意味着我们必须自己对其进行URL编码。
因此,如果我指定内容类型,请将参数转换为字符串并使用urllib进行编码,然后将参数传递给请求:
head['Content-Type'] = 'application/x-www-form-urlencoded'
in_dict = urllib.quote(json.dumps(in_dict))
r = requests.post(url, data=in_dict, headers=head)
然后请求成功。
您发送的数据必须是JSON编码。
从文档(您链接到的页面):
要调用CKAN API,请在HTTP POST请求中发布JSON字典到CKAN的API URL之一。
在urllib示例中,这是通过以下代码行执行的:
data_string = urllib.quote(json.dumps(dataset_dict))
我认为(尽管您应该检查)requests
库将为您提供报价-因此您只需要将dict转换为JSON。这样的事情应该起作用:
r = requests.post(d_url, data=json.dumps(dataset_dict), headers=auth)
我是Python和CKAN的新手。我已经安装了Python 3.8,文件无法正常工作。您能否给我语法使它在Python 3中起作用(文件在Python 3中不起作用)
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)