无法使用python请求发布文件+数据

编程入门 行业动态 更新时间:2024-10-07 14:34:00
本文介绍了无法使用python请求发布文件+数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以使用curl张贴文件

I'm able to post file using curl

curl -X POST -i -F name='barca' -F country='spain' -F file=@/home/messi/Desktop/barca.png 'localhost:8080/new_org/hel/concerts'

我可以得到(文件)为

curl -X GET -H 'Accept: image/png' 'localhost:8080/new_org/hel/concerts/<id or name of entity>'

但是当我使用 requests.post 尝试相同的操作时,出现了错误。有谁知道为什么会这样。 (当文件指针不在最后,但是文件指针在最后时,遇到Post Post遇到错误200,但文件没有发布)

But when I tried same thing using requests.post, I got error. Does anybody know why this happen. (Post Error encounter when file pointer is not at last, but when file pointer is at last, I got response 200 but file is not posted)

import requests url = 'localhost:8080/new_org/hel/concerts' file = dict(file=open('/home/messi/Desktop/barca.png', 'rb')) data = dict(name='barca', country='spain') response = requests.post(url, files=file, data=data)

错误:(来自 usergrid ),其响应代码为:400

Error: (from usergrid) with response code: 400

{u'duration': 0, u'error': u'illegal_argument', u'error_description': u'value is null', u'exception': u'java.lang.IllegalArgumentException', u'timestamp': 1448330119021}

github/apache/usergrid

推荐答案

我相信问题是Python没有发送您要发送的图像的内容类型字段。我使用调试器跟踪了Usergrid代码,发现curl 是发送图像的内容类型,而Python不是。

I believe the problem is that Python is not sending a content-type field for the image that you are sending. I traced through the Usergrid code using a debugger and saw that curl is sending the the content-type for the image and Python is not.

我能够获得此确切的代码以在我的本地Usergrid上运行:

I was able to get this exact code to work on my local Usergrid:

import requests url = '10.1.1.161:8080/test-organization/test-app/photos/' files = { 'file': ('13.jpg', open('/Users/dave/Downloads/13.jpg', 'rb'), 'image/jpeg')} data = dict(name='barca', country='spain') response = requests.post(url, files=files, data=data)

由于文件的语法,Waken Meng的答案可能无法正常工作变量,但我不是Python专家。

It is possible that Waken Meng's answer did not work because of the syntax of the files variable, but I'm no Python expert.

更多推荐

无法使用python请求发布文件+数据

本文发布于:2023-10-10 18:09:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   数据   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!