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

239
Views
How do I post Form Data with aiohttp?

I am testing making an OAuth server with FastAPI. I have one side, which is the Oauth server which has an endpoint that looks like:

@router.post("/token")
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()) -> Dict[str, str]:
    return {"access_token": form_data.username, "token_type": "bearer"}

And then on a separate process, I have a fake app with a log-in endpoint which looks like:

@router.post("/")
async def login(username: str, password: str) -> Dict[str, str]:

    form = OAuth2PasswordRequestForm(
        username=username,
        password=password,
        scope="me"
    )

    form_data = aiohttp.FormData()

    for key, val in form.__dict__.items():
        form_data.add_field(key, val)

    async with aiohttp.ClientSession() as session:
        async with session.post(f"http://localhost:8001/oauth/token", data=form_data()) as server_response:
            response = await server_response.text()
            response = json.loads(response)
    return response

The OAuth server is responding with

{
  "detail": [
    {
      "loc": [
        "body",
        "grant_type"
      ],
      "msg": "string does not match regex \"password\"",
      "type": "value_error.str.regex",
      "ctx": {
        "pattern": "password"
      }
    }
  ]
}

How can I use aiohttp to post the form data to the oauth server?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The grant_type field is missing from your data. Although grant_type is an optional field, as per the documentation (have a look under the "Tip" section):

The OAuth2 spec actually requires a field grant_type with a fixed value of password, but OAuth2PasswordRequestForm doesn't enforce it.

If you need to enforce it, use OAuth2PasswordRequestFormStrict instead of OAuth2PasswordRequestForm.

Thus, your form should look like the one below. The grant_type="password" means that you are sending a username and a password to the /token endpoint (have a look at this answer as well).

form = OAuth2PasswordRequestForm(
    grant_type="password",
    username=username,
    password=password,
    scope="me"
)
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!