Alo*_*ngh 1 flask-sqlalchemy flask-restful
我正在 python Flask 中创建一条路线,它将作为其余 api 来注册用户。当我尝试在 POST 方法中获取通过邮递员传递的 json 数据时,出现错误TypeError: 'NoneType' object is not subscriptable
我来自邮递员的请求:
http://127.0.0.1:5000
/register 原始输入:{"username":"alok","password":"1234"}
我的路线和功能:
@app.route('/register', methods=['GET', 'POST'])
def signup_user():
data = request.get_json()
return data['username']
Run Code Online (Sandbox Code Playgroud)
据我所知,上述函数应该返回:“ alok ”
但我收到错误:TypeError: 'NoneType' object is not subscriptable
任何帮助将不胜感激
我在网上花了几个小时我从烧瓶官方网站得到了答案
我在发出请求时没有设置 mimetype。
\n如果 mimetype 不指示 JSON(application/json,请参阅 is_json()),则返回 None。\n
\n request.get_json() 用于将数据解析为 JSON。\n实际语法是get_json(force=False, silent=False, cache=True)
参数
\ nforce \xe2\x80\x93 忽略 mimetype 并始终尝试解析 JSON。\n
\ nsilent \xe2\x80\x93 静默解析错误并返回 None。\n
\n缓存\xe2\x80\x93存储解析后的 JSON 以返回以供后续调用。\n
所以最后我将代码更改为
\n\n@app.route('/register', methods=['GET', 'POST'])\ndef signup_user():\n data = request.get_json(force=True)\n return data['username']\nRun Code Online (Sandbox Code Playgroud)\n\n已解决。
\n| 归档时间: |
|
| 查看次数: |
3650 次 |
| 最近记录: |