转换' curl -F'到Python请求

时间:2015-08-12 22:31:13

标签: python curl python-requests restheart

我尝试使用"请求"进行多部分表单上传。在python中。我可以上传文件,但它永远不会加载通常使用curl通过-F标志传递的其他字段。卷曲命令我试图模仿:

curl -v -u user:password -X POST -F 'properties={"filename":"maurizio.png"}' -F "file=@maurizio.png" 192.168.59.103:8080/testdb/mybucket.files

请求代码是:

url = "http://192.168.59.103:8080/testdb/mybucket.files"
content_type = "multipart/form-data"

params = {"filename":"maurizio.png"}
params["_id"] = "test_hash_1234" 

data = open('maurizio.png','rb').read()

files = {'file': ('maurizio.png',data,content_type,params)}               
request = requests.post(url,files=files,auth=('user','password'))

此文件被加载到mongodb的restheart api中。 " _id"是数据库用于引用对象的特殊对象ID。我需要在文件名之外的其他文档字段中指定该文档字段。该文件被上传到名为gridfs的Mongo的blob数据存储区中。我可以使用curl上传数据和其他文档字段,但是"请求"上面的代码只上传数据而不上传其他文档字段。我需要那些额外的字段来查询。

有人可以帮助我将-F标志转换为与python的请求模块等效的东西吗?谢谢!

2 个答案:

答案 0 :(得分:0)

查看example in the manual

thing := array_to_string(ARRAY(select * from some_table limit 1 ));
raise info 'insert result: %',thing;

请注意,首先应该传递文件而不是数据(您不需要files = {'file': ('report.xls', open('report.xls', 'rb'))} r = requests.post(url, files=files) 文件内容,只需要传递文件对象)

答案 1 :(得分:0)

顺便说一句,还有一个Github repository,它显示了使用Python访问RESTHeart的一些基本示例。

目标是展示如何使用Python和requests library进行简单的RESTful调用,请随意添加更多案例的pull请求,或者只是看看现有的测试是否可以提供帮助你。