无法使用 python-请求 post 文件+数据
unable to post file+data using python-requests
我可以使用 curl post 文件
curl -X POST -i -F name='barca' -F country='spain' -F
file=@/home/messi/Desktop/barca.png 'http://localhost:8080/new_org/hel/concerts'
我可以获得(文件)作为
curl -X GET -H 'Accept: image/png' 'http://localhost:8080/new_org/hel/concerts/<id or name of entity>'
但是当我使用 requests.post
尝试同样的事情时,我得到了错误。有谁知道为什么会这样。 (Post 当文件指针不是最后时遇到错误,但是当文件指针最后时,我得到响应 200 但文件不是 posted)
import requests
url = 'http://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
{u'duration': 0,
u'error': u'illegal_argument',
u'error_description': u'value is null',
u'exception': u'java.lang.IllegalArgumentException',
u'timestamp': 1448330119021}
我之前在尝试上传图像文件时遇到了问题。然后我读了 doc 并做了这部分:
You can set the filename, content_type and headers explicitly:
我是这样定义 file_data:
file_data = [('pic', ('test.png', open('test.png'), 'image/png'))]
r = requests.post(url, files=file_data)
file_data 应该是一个列表:[(param_name, (file_name, file, content_type))]
这个对我有用,希望能帮到你。
我认为问题在于 Python 没有为您发送的图像发送内容类型字段。我使用调试器跟踪了 Usergrid 代码,发现 curl 是 发送图像的内容类型,而 Python 不是。
我能够在我的本地 Usergrid 上获得准确的代码:
import requests
url = 'http://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)
可能是因为 files 变量的语法,Waken Meng 的回答没有起作用,但我不是 Python 专家。
我可以使用 curl post 文件
curl -X POST -i -F name='barca' -F country='spain' -F
file=@/home/messi/Desktop/barca.png 'http://localhost:8080/new_org/hel/concerts'
我可以获得(文件)作为
curl -X GET -H 'Accept: image/png' 'http://localhost:8080/new_org/hel/concerts/<id or name of entity>'
但是当我使用 requests.post
尝试同样的事情时,我得到了错误。有谁知道为什么会这样。 (Post 当文件指针不是最后时遇到错误,但是当文件指针最后时,我得到响应 200 但文件不是 posted)
import requests
url = 'http://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
{u'duration': 0,
u'error': u'illegal_argument',
u'error_description': u'value is null',
u'exception': u'java.lang.IllegalArgumentException',
u'timestamp': 1448330119021}
我之前在尝试上传图像文件时遇到了问题。然后我读了 doc 并做了这部分:
You can set the filename, content_type and headers explicitly:
我是这样定义 file_data:
file_data = [('pic', ('test.png', open('test.png'), 'image/png'))]
r = requests.post(url, files=file_data)
file_data 应该是一个列表:[(param_name, (file_name, file, content_type))]
这个对我有用,希望能帮到你。
我认为问题在于 Python 没有为您发送的图像发送内容类型字段。我使用调试器跟踪了 Usergrid 代码,发现 curl 是 发送图像的内容类型,而 Python 不是。
我能够在我的本地 Usergrid 上获得准确的代码:
import requests
url = 'http://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)
可能是因为 files 变量的语法,Waken Meng 的回答没有起作用,但我不是 Python 专家。