Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

462
Views
FastAPI OAuth2PasswordRequestForm dependency causing request failure

I'm using the auth scheme detailed in FastAPI's user guide (JWT/Bearer token). When I try to get a token from the /token endpoint the request fails before the path operation function ever runs. Here's the function in question:

async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), session: SessionLocal = Depends(get_db)):
    user = authenticate_user(session, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token = create_access_token(data={"sub": user.id})
    return {"access_token": access_token, "token_type": "bearer"}

I normally use breakpoints to help figure out what's going wrong in these situations, but the call fails before any code in the function actually runs, leading me to believe the problem is with the OAuth2PasswordRequestForm dependency. I verified that this was the problem by disabling the form_data parameter and was able to execute the full request.

The errors I'm getting are pretty sparse on details. Here's what I'm getting in the console: INFO: 127.0.0.1:52261 - "POST /token HTTP/1.1" 400 Bad Request

And here's what I see in the Swagger UI:

enter image description here

This was working for me in the single-file format used in the tutorial, but I've since broken things out into different files to keep my larger project organized. I have a feeling that I just missed something somewhere along the line while reorganizing this code but can't seem to figure out what is causing this.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Of course the solution to this issue was in the docs. The problem was that I was missing the python-multipart dependency. I just ran pip install python-multipart and restarted my server and then I was able to authenticate.

over 4 years ago · Santiago Trujillo Report

0

You need to set response_model to token after you have created a token schema like this:

class Token(BaseModel):
    access_token: str
    token_type: str

so your code should look like this

@router.post('/token', response_body=Token)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!