FastAPI accepting a POST with BasicAuth, Headers and Body?

Question:

So relatively new to FastAPI, and I have a small project I am trying to perform. Been through the examples at https://fastapi.tiangolo.com/. and did get some of what I am posting but not all.

My Post is a JSON body, with a single header, but uses basic auth. If I use the FastAPI Basic Auth example, I can get the user/pass easily. If I use Request, It gets the Header and Body easily, but accessing the user data coughs

    ), "AuthenticationMiddleware must be installed to access request.user"
AssertionError: AuthenticationMiddleware must be installed to access request.user

So I can see it’s there, but can’t read it.

from typing import Union
from fastapi import Depends, FastAPI, Header, Request, Body
from fastapi.security import HTTPBasic, HTTPBasicCredentials
security = HTTPBasic()
app = FastAPI()
@app.post("/posttest")
async def root(request: Request):
        my_header = request.headers.get('client_id')
        return{"Client-Id": my_header, "Body": await request.json(), "User": request.user}

I am not sure how one collects all 3 bits so I can use my own python request code to do a proper O-Auth2 call with this data to return the answer the original post needed (The App cannot do O-Auth, so I am making a wedge)

This block instead will get the username and password, but I am unable to pull my Header or Body. I am sure I am missing something silly, but this is my first crack at this.

async def root(credentials: HTTPBasicCredentials = Depends(security)):
        return {"username": credentials.username, "password": credentials.password}
Asked By: Nick Ellson

||

Answers:

Learned my solution by learning how to build on the attributes in my app’s funtion. Thanks everyone!

def read_items(credentials: HTTPBasicCredentials = Depends(security), client_id: Union[str, None] = Header(default=None, convert_underscores=False), item: str = Body(embed=True)):
Answered By: Nick Ellson