Bird
0
0

Given this FastAPI code snippet using OAuth2 password flow, what will be the response if the username is 'user1' and password is 'wrongpass'?

medium📝 Predict Output Q4 of 15
FastAPI - Authentication and Security
Given this FastAPI code snippet using OAuth2 password flow, what will be the response if the username is 'user1' and password is 'wrongpass'? ```python from fastapi import FastAPI, Depends, HTTPException from fastapi.security import OAuth2PasswordRequestForm app = FastAPI() @app.post('/token') async def login(form_data: OAuth2PasswordRequestForm = Depends()): if form_data.username == 'user1' and form_data.password == 'pass123': return {'access_token': 'token123', 'token_type': 'bearer'} raise HTTPException(status_code=400, detail='Incorrect username or password') ```
A{'access_token': 'token123', 'token_type': 'bearer'}
BHTTP 401 error with detail 'Unauthorized'
C{'access_token': 'wrongpass', 'token_type': 'bearer'}
DHTTP 400 error with detail 'Incorrect username or password'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the login condition

    The code returns a token only if username is 'user1' and password is 'pass123'.
  2. Step 2: Check given input against condition

    The password 'wrongpass' does not match 'pass123', so the condition fails and raises HTTPException with status 400.
  3. Final Answer:

    HTTP 400 error with detail 'Incorrect username or password' -> Option D
  4. Quick Check:

    Wrong password = HTTP 400 error [OK]
Quick Trick: Wrong password triggers HTTP 400 error [OK]
Common Mistakes:
MISTAKES
  • Assuming token is returned despite wrong password
  • Confusing HTTP 400 with 401 Unauthorized
  • Expecting password to appear in token

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes