简体   繁体   English

在 Python 中通过 Post 方法上传 Turtle 文件

[英]Upload Turtle files by Post method in Python

I am trying to send a turtle file via a Python script using REST api to the local repository but the System is returning the following error我正在尝试使用 REST api 通过 Python 脚本将海龟文件发送到本地存储库,但系统返回以下错误

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

400 400

The used code is as follows:使用的代码如下:

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)

The same file when tried with a Curl command is working fine:尝试使用 Curl 命令时,相同的文件工作正常:

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

Any idea regarding this issue is welcome欢迎任何关于这个问题的想法

I think your problem come from the way you pass your file to the post request.我认为您的问题来自您将文件传递给发布请求的方式。 You should try using the data parameter of the post request.您应该尝试使用发布请求的数据参数。 For example:例如:

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM