API REST 猎鹰 python POST 方法
API REST falcon python POST METHOD
我尝试使用 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 (raw -> JSON)
中使用以下正文
{
"email":"test"
}
我错过了什么?
首先,使用req.media
检索数据(建议)。
其次,对于响应,使用 resp.body = json.dumps({"test": "answer"})
所述
您可以使用 media
使用请求原始正文 JSON 并将 media
附加到响应。
Zero configuration is needed if you’re creating a JSON API. Just
access or set the media attribute as appropriate and let Falcon do the
heavy lifting for you.
class EchoResource:
def on_post(self, req, resp):
message = req.media.get("message")
resp.media = {
"message": message
}
resp.status = falcon.HTTP_200
Warning:
Once media is called on a request, it’ll consume the request’s
stream.
我尝试使用 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 (raw -> JSON)
中使用以下正文{
"email":"test"
}
我错过了什么?
首先,使用req.media
检索数据(建议)。
其次,对于响应,使用 resp.body = json.dumps({"test": "answer"})
您可以使用 media
使用请求原始正文 JSON 并将 media
附加到响应。
Zero configuration is needed if you’re creating a JSON API. Just access or set the media attribute as appropriate and let Falcon do the heavy lifting for you.
class EchoResource:
def on_post(self, req, resp):
message = req.media.get("message")
resp.media = {
"message": message
}
resp.status = falcon.HTTP_200
Warning:
Once media is called on a request, it’ll consume the request’s stream.