Bird
0
0

Identify the error in this JWT token creation code snippet:

medium📝 Debug Q14 of 15
FastAPI - Authentication and Security
Identify the error in this JWT token creation code snippet:
from jwt import encode

payload = {"user_id": 42}
secret = "secretkey"
token = encode(payload, secret)
print(token)
AMissing algorithm parameter causes an error
BNo error; code runs correctly
CSecret key should be bytes, not string
DPayload must be a string, not a dictionary
Step-by-Step Solution
Solution:
  1. Step 1: Check encode function requirements

    PyJWT's encode has a default algorithm='HS256', so it is not strictly required.
  2. Step 2: Analyze the code snippet

    The code calls encode with payload and secret; algorithm defaults to HS256, so it runs correctly and produces a token.
  3. Final Answer:

    No error; code runs correctly -> Option B
  4. Quick Check:

    Algorithm defaults to HS256 = no error [OK]
Quick Trick: PyJWT encode defaults to HS256 algorithm [OK]
Common Mistakes:
MISTAKES
  • Assuming algorithm defaults to HS256
  • Thinking payload must be string
  • Believing secret must be bytes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes