Create CKAN dataset using CKAN API and Python Requests library

后端 未结 2 1489
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 03:25

I am using CKAN version 2.2 and am trying to automate dataset creation and resource upload. I seem to be unable to create a dataset using the python requests libra

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 03:52

    I finally came back to this and figured it out. Alice's suggestion to check the encoding was very close. While requests does do the encoding for you, it also decides on its own which type of encoding is appropriate depending on the inputs. If a file is passed in along with a JSON dictionary, requests automatically does multipart/form-data encoding which is accepted by CKAN therefore the request is successful.

    However if we pass only a JSON dictionary the default encoding is form encoding. CKAN needs requests without files to be URL encoded (application/x-www-form-urlencoded). To prevent requests from doing any encoding we can pass our parameters in as a string then requests will perform only a POST. This means we have to URL encode it ourselves.

    Therefore if I specify the content type, convert the parameters to a string and encode with urllib and then pass the parameter to requests:

    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)
    

    Then the request is successful.

提交回复
热议问题