Mee*_*ers 5 rest python-3.x gunicorn
我正在尝试读取带有 JSON 数据的简单请求正文。
请求正文:
[
{
...data
},
{
...data
}
]
Run Code Online (Sandbox Code Playgroud)
当我尝试时(在EventResource)
def on_post(self, req, resp):
print(req.stream.read())
Run Code Online (Sandbox Code Playgroud)
以下是登录到控制台的信息:b''
我不知道我做错了什么,也不知道为什么它不显示我的身体数据。我在执行此操作时看到的每个示例实际上都会记录数据而不是我得到的数据。
requirements.txt(可能有些断章取义,但为了确定起见,我添加了完整列表。)
astroid==1.5.3
bson==0.5.0
cffi==1.11.2
click==6.7
falcon==1.4.1
falcon-auth==1.1.0
falcon-jsonify==0.1.1
Flask==0.12.2
greenlet==0.4.12
gunicorn==19.7.1
isort==4.2.15
itsdangerous==0.24
Jinja2==2.10
lazy-object-proxy==1.3.1
MarkupSafe==1.0
mccabe==0.6.1
mimeparse==0.1.3
mongoengine==0.15.0
pycparser==2.18
PyJWT==1.5.3
pylint==1.7.4
pymongo==3.5.1
python-mimeparse==1.6.0
pytz==2017.3
readline==6.2.4.1
six==1.11.0
Werkzeug==0.12.2
wrapt==1.10.11
Run Code Online (Sandbox Code Playgroud)
应用程序.py
api = falcon.API(middleware=[
falcon_jsonify.Middleware(help_messages=settings.DEBUG)
])
Run Code Online (Sandbox Code Playgroud)
路线.py
from app import api
from resources.event import EventResource
from resources.venue import VenueResource
# EventResources
api.add_route('/api/event', EventResource())
api.add_route('/api/event/{event_id}', EventResource())
api.add_route('/api/venue/{venue_id}/events', EventResource())
# VenueResources
api.add_route('/api/venue', VenueResource())
api.add_route('/api/venue/{venue_id}', VenueResource())
api.add_route('/api/event/{event_id}/venue', VenueResource())
Run Code Online (Sandbox Code Playgroud)
我运行我的项目gunicorn routes:api --reload
示例 POST 请求(记录b''):
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:8000/api/event
Run Code Online (Sandbox Code Playgroud)
我作为标题添加的唯一内容是Content-Type/application/json
我已经读过这篇文章,但它对我没有帮助。
这种行为的发生是因为你
falcon_jsonify.Middleware(help_messages=settings.DEBUG)
Run Code Online (Sandbox Code Playgroud)
该流已被它读取。req.json在这种情况下你需要使用。如果删除中间件,则会req.stream.read()正确返回值。如果你看看中间件的process_request方法
def process_request(self, req, resp):
if not req.content_length:
return
body = req.stream.read()
req.json = {}
self.req = req
req.get_json = self.get_json
try:
req.json = json.loads(body.decode('utf-8'))
except ValueError:
self.bad_request("Malformed JSON", "Syntax error")
except UnicodeDecodeError:
self.bad_request("Invalid encoding", "Could not decode as UTF-8")
Run Code Online (Sandbox Code Playgroud)
您可以看到中间件读取主体,然后吐出req.json与解析对象相同的内容。但原来的身体并没有保存在其他地方。一旦读取请求流,您就清空了其缓冲区,并且不会再次获取数据。因此你得到b''
| 归档时间: |
|
| 查看次数: |
2648 次 |
| 最近记录: |