首页 > 解决方案 > 在 Python 中通过 Post 方法上传 Turtle 文件

问题描述

我正在尝试使用 REST api 通过 Python 脚本将海龟文件发送到本地存储库,但系统返回以下错误

错误数据:非法主题值:“-”^^ http://www.w3.org/2001/XMLSchema#integer [第 1 行]

400

使用的代码如下:

import requests

url = 'http://localhost:7200/repositories/metaphactory1/statements'
with open("graph-29.ttl", "rb") as ttl_file:
    file_dict = {"graph-29.ttl" : ttl_file}
    headers = {
        "Content-type": "application/x-turtle;charset=UTF-8",
    }

    r = requests.post(url, files=file_dict, headers=headers)
    print(r.text)

    print(r.status_code)

尝试使用 Curl 命令时,相同的文件工作正常:

 curl -X POST -H "Content-Type: application/x-turtle" -T graph-29.ttl 'http://localhost:7200/repositories/metaphactory1/statements'

欢迎任何关于这个问题的想法

标签: pythonrestgraphdbturtle-rdfrdf4j

解决方案


我认为您的问题来自您将文件传递给发布请求的方式。您应该尝试使用发布请求的数据参数。例如:

import requests

url = 'http://localhost:7200/repositories/metaphactory1/statements'
file_path = '/path/to/your/file/graph-29.ttl'
graph_name = 'http://graph-29'
headers = {
  'Content-type': 'application/x-turtle',
}

params = {'graph': graph_name} #optional
response = requests.post(url, headers=headers, params=params, data=open(file_path,'r', encoding='utf-8').read())

推荐阅读