我使用python-eve构建我的RESTful API,我设置了一个端点' risk'在settings.py中,如下所示:
risks = {
'schema': {
'description': {
'type': 'string',
'required': True,
},
'pic': {
'type': 'list',
'schema': {
'type': 'media',
}
},
}
}
我尝试使用curl命令将两个jpg文件POST到' pic'像这样的领域:
curl -F "description=something" -F "pic=@test1.jpg,test2.jpg" http://127.0.0.1:8080/risks
但是像这样得到422错误:
{"_status": "ERR", "_issues": {"pic": "must be of list type"}, "_error": {"message": "Insertion failure: 1 document(s) contain(s) error(s)", "code": 422}}%
我能做什么,我只试过一种媒体(不是列表类型),没问题,为什么? 有人能帮助我吗?
答案 0 :(得分:1)
当您访问risks
端点时,您需要提供正确的数据类型,以便资源端点能够正常工作。它抱怨你发送的图片,因为它仍然是一个字符串而不是列表。
使用requests
import requests
url = 'http://127.0.0.1:8080/risks'
data = {description : 'something', 'pic' : ['a','b','c','d']}
r = requests.post(url, json=data)
答案 1 :(得分:0)
@Mike Tung
...
使用请求
可以让您的生活更轻松...
如何将文件传递给请求json
kwarg?作为文件处理?还是字符串?
当我尝试这个时,
import requests
url = 'http://localhost:5000/tests'
data = {
'icons': [
open('/home/test/git-projects/test.com/backend/fixtures/media/test1.jpg').read(), # If I pass the handle, it fails as well.
open('/home/test/git-projects/test.com/backend/fixtures/media/test2.jpg').read()
]
}
r = requests.post(url, json=data)
/home/test/venvs/test/bin/python2.7 /home/test/git-projects/test.com/backend/__main__.py
/home/test/venvs/test/local/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.pymongo is deprecated, use flask_pymongo instead.
.format(x=modname), ExtDeprecationWarning
/home/test/venvs/test/local/lib/python2.7/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.sentinel is deprecated, use flask_sentinel instead.
.format(x=modname), ExtDeprecationWarning
Traceback (most recent call last):
File "/home/test/git-projects/test.com/backend/__main__.py", line 43, in <module>
r = requests.post(url, json=data)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/api.py", line 109, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/sessions.py", line 451, in request
prep = self.prepare_request(req)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/sessions.py", line 382, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/models.py", line 307, in prepare
self.prepare_body(data, files, json)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/requests/models.py", line 428, in prepare_body
body = json_dumps(json)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/simplejson/__init__.py", line 380, in dumps
return _default_encoder.encode(obj)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/simplejson/encoder.py", line 275, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/test/venvs/test/local/lib/python2.7/site-packages/simplejson/encoder.py", line 357, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
Process finished with exit code 1