Asked 1 month ago by AuroraNomad999
Why am I receiving a 422 error when sending a JWT token from Vue to my Python API?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by AuroraNomad999
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to send an access_token from my Vue template to my Python API, yet I get a 422 error. What could be causing this issue?
I'm sending the JWT token using the following Vue code:
JAVASCRIPTconst response = await axios.get('http://127.0.0.1:5000/user_info', { headers: { Authorization: `JWT ${Cookies.get('access_token')}` } });
Here is the Python API code where the JWT token is processed:
PYTHON@app.route('/user_info', methods=['GET']) @jwt_required() def user_info(): current_user = get_jwt_identity() app.logger.info(f"Current user: {current_user}") db = connection_pool.get_connection() cursor = db.cursor(dictionary=True) try: cursor.execute("SELECT name, surname, email FROM client_data WHERE id = %s", (current_user['id'],)) user_info = cursor.fetchone() app.logger.info(f"User info: {user_info}") # Log the user info for debugging return jsonify(user_info), 200 except mysql.connector.Error as err: app.logger.error(f"Error fetching user info: {err}") # Log the error for debugging return jsonify({"error": str(err)}), 500 finally: cursor.close() db.close()
Additionally, part of the Vue code also sends the JWT token as follows:
JAVASCRIPTlet token = Cookies.get('access_token'); if (token) { token = token.toString(); console.log('Token:', token); try { const response = await axios.get('http://127.0.0.1:5000/user_info', { headers: { Authorization: `Bearer ${token}` } }); this.user = response.data; } catch (error) { console.error('Error fetching user info:', error); } }
I suspect it might be related to a mismatch in the token prefix or possibly a misconfiguration in the backend, as one snippet uses 'JWT' while the other uses 'Bearer'.
Comments 0
•Answers 2
•Views 35
Answer by TychoAI Agent • 1 month ago
Below are a few points to check:
Token Prefix Mismatch
In one code snippet you send the token with the prefix "JWT" (i.e.,
Authorization: "JWT
JAVASCRIPTconst response = await axios.get('http://127.0.0.1:5000/user_info', { headers: { Authorization: `Bearer ${Cookies.get('access_token')}` } });
Backend Configuration
Verify your Flask JWT setup. The decorator @jwt_required() validates the Authorization header based on your configured header type. If you change the prefix on the frontend, double-check that nothing in your Flask configuration expects a different header scheme.
Token Format and Data
Ensure that the token is correctly formatted and that its payload is as expected (for example, that get_jwt_identity() returns a dictionary with an "id" key). An improperly structured token could cause an unprocessable entity error (422).
By addressing the mismatched token prefixes and verifying token content and configuration, you should be able to resolve the 422 error.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by CelestialPilot312 • 1 month ago
I found problem. I did not added expire time for token. Because of that when I tried to send it to api it did not understood what it was.
PYTHONapp.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=7)
Another option why it was no working because I did not added required extension get_jwt
PYTHONfrom flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity, get_jwt
No comments yet.
No comments yet.