1

I've searched throughly on stack overflow and google about this error and I couldn't find the solution that resolves my situation.

I'm using marshmallow to serialize/deserialize my api get request.

@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)

***** : when I omit this line, this works fine, the only reason that I'm adding that line is to show whoever logged in his/her profile instead of showing everyone's profile. $$$$$ : This is the line of error.

Full 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)

I do not understand why this is happening, the ****** line is not directly showing its error after I put @login_required right above def get_profile. But when the @login_required was not there, it was throwing

AttributeError: 'AnonymousUser' object has no attribute 'email'

If possible I would like to know a way of getting current user's email so I can query current user's profile instead of whole. Also, I really want to know why below error is occurring.

 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

Thanks,

2 Answers 2

3

This is what I think is happening:

You are making a request to your own server to an endpoint with the @login_required decorator (I'm assuming you are using flask-login or similar). This creates a different session where the 'current user' you are expecting to be logged in is not actually authenticated (you are probably receiving a 401 response, unauthorized).

If you want the response from a different endpoint without losing the currently authenticated user, you can try something like this:

@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)
Sign up to request clarification or add additional context in comments.

2 Comments

@ AArias: I get "TypeError: the JSON object must be str, not 'bytes'" , since HTTP response returns bytes not strings, I tried to decode it using this stackoverflow answer stackoverflow.com/questions/6862770/…, but it is still throwing an same error
your updated solution works perfectly. Nice and clean, thanks
2

Ok so I found the solution, I'm giving all my credit to AArias, since he suggested a more clean way of get the response data. So since HTTP returns bytes not strings, you need to explicitly decode it into utf-8, and many of the existing synthax does not work.. I don't know why. The below code works.

response = get_profile().data
data = json.loads(response.decode('utf-8'))

If someone try to decode it with .read() or .readall(), it would not work because the response object does not have read or readall method. The best way is the simplest way, just decode it upfront and use 'loads' , not 'load'. Hope it helps someone

2 Comments

Glad you found the answer. Actually there's a better way to get the response as a unicode string without using the decode method on the response. Simply use the get_data() method. The data attribute will actually be eventually deprecated, as stated here: flask.pocoo.org/docs/0.12/api/#flask.Response.data Sorry about that, my bad. You should change .data to .get_data(as_text=True) and then you should be able to avoid using .decode('utf-8'). I updated my answer to reflect this, please let me know if it works!
Why unaccept my answer if it is still valid, 9 months later, and actually solved your issue? Not cool, since I took the time to help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.