我已经仔细搜索了堆栈溢出并谷歌关于此错误,我找不到解决我的情况的解决方案。
我正在使用marshmallow序列化/反序列化我的api get请求。
@post_user_blueprint.route('/api/v1/get_current_profile', methods=['GET'])
@login_required
def get_profile():
query_user_by_order = User.query.join(Sample) \
.join(Individual) \
.join(Ordering) \
.join(Family) \
.join(Sequence) \
.filter(User.id == Sample.fk_user_id) \
.filter(Individual.id == Sample.fk_individual_id) \
.filter(Sample.id == Sequence.fk_sample_id) \
.filter(Individual.id == Ordering.fk_individual_id) \
.filter(Family.id == Individual.fk_family_id) \
.filter(User.email == current_user.email)\ ***********
.all()
result = profile_users_schema.dump(query_user_by_order)
return jsonify({'user': result.data})
@login_required
@post_user_blueprint.route('/profile', methods=['GET', 'POST'])
def profile():
response = requests.get('http://127.0.0.1:5000/api/v1/get_current_profile')
response = response.text
data = json.loads(response) $$$$$$$$$$$$$$$$$
return render_template('profile.html', user_object = data)
*****:当我省略这一行时,这个工作正常,我添加该行的唯一原因是显示谁登录他/她的个人资料而不是显示每个人的个人资料。 $$$$$:这是错误行。
完整TraceBack
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/genomics/PycharmProjects/sample_accessioning/app/views/post_inputs.py", line 209, in profile
profile = response.json()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/requests/models.py", line 826, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我不明白为什么会发生这种情况,在我将@login_required放在def get_profile之上后,******直线并没有直接显示错误。但是当@login_required不存在时,它就抛出了
AttributeError: 'AnonymousUser' object has no attribute 'email'
如果可能,我想了解获取当前用户电子邮件的方法,以便我可以查询当前用户的个人资料而不是整个。另外,我真的想知道为什么会出现以下错误。
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
谢谢,
答案 0 :(得分:1)
这就是我的想法:
您正在使用@login_required
装饰器向您自己的服务器发送请求到一个端点(我假设您正在使用flask-login或类似的东西)。这会创建一个不同的会话,其中您希望登录的“当前用户”实际上未经过身份验证(您可能正在接收401响应,未经授权)。
如果您希望来自不同端点的响应而不会丢失当前经过身份验证的用户,则可以尝试以下操作:
@login_required
@post_user_blueprint.route('/profile', methods=['GET', 'POST'])
def profile():
response = get_profile().get_data(as_text=True)
data = json.loads(response)
return render_template('profile.html', user_object = data)
答案 1 :(得分:1)
好的,所以我找到了解决方案,我将所有的功劳都归功于AArias,因为他提出了一种更简洁的方法来获取响应数据。因此,由于HTTP返回字节而不是字符串,您需要将其显式解码为utf-8,并且许多现有的synthax不起作用..我不知道为什么。 以下代码有效。
response = get_profile().data
data = json.loads(response.decode('utf-8'))
如果有人尝试使用.read()或.readall()对其进行解码,则无法使用,因为响应对象没有read或readall方法。最好的方法是最简单的方法,只需先解码并使用“加载”即可。 ,而不是“负载”。 希望它可以帮助某人