python - 在 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'
欢迎任何关于这个问题的想法
解决方案
我认为您的问题来自您将文件传递给发布请求的方式。您应该尝试使用发布请求的数据参数。例如:
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())
推荐阅读
- string - 如何在一行中加入两个字符串 discord bot
- java - 将对象添加到场景条件 Java3D
- flutter - 在 Flutter 中处理用户会话是否有比使用“flutter_session”或“local_storage”更好的方法,或者无法在移动设备上处理?
- asp.net-core - 通过 web.config 切换到新的应用程序版本
- python - Python 读取 JSON 文件中的多个值作为字典
- php - 每次我重新加载我的网站时,它都会在我的 JSON 文件中添加一个新用户?我怎样才能防止这种情况?
- python - 我找不到正确循环这个刽子手代码的方法
- c# - 获取经过身份验证的用户的全名
- php - while 循环从数字 934 中减去 6.3,直到数字 * 介于(不等于)47 和 57 之间
- ruby-on-rails - 如何在邮件程序中使用模板变量