我尝试使用 falcon 编写 API REST。
on_get 方法效果很好,但是在使用 on_post 时,我无法获取 POST 请求的正文,我不知道为什么
class ProfileUpdate(object):
def on_post(self, req, resp):
data = json.load(req.stream)
print(data)
resp.body = {"test": "answer"}
resp.status = falcon.HTTP_200
return resp
def setup_profile():
app = falcon.API()
profile_update = ProfileUpdate()
app.add_route('/profiles', profile_update)
return app
我收到以下错误
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/usr/local/lib/python3.7/site-packages/falcon/api.py", line 318, in __call__
body, length = self._get_body(resp, env.get('wsgi.file_wrapper'))
File "/usr/local/lib/python3.7/site-packages/falcon/api.py", line 820, in _get_body
body = body.encode('utf-8')
AttributeError: 'dict' object has no attribute 'encode'
我正在使用 Postman 来测试 API。我尝试在 POSTMAN 中使用以下正文(原始-> JSON)
{
"email":"test"
}
我错过了什么?