简体   繁体   中英

Errror parsing data in python FastAPI

I'm learning to use FastAPI, and I'm getting this error over and over again while implementing a simple API and I've not being able to figure out why

"detail": "There was an error parsing the body"

This happends me on this two endpoints:

Full code: Code Repository

snippet:

app_v1 = FastAPI(root_path='/v1')

# JWT Token request
@app_v1.post('/token')
async def login_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
    jwt_user_dict = {"username": form_data.username, "password": form_data.password}
    jwt_user = JWTUser(**jwt_user_dict)
    user = authenticate_user(jwt_user)
    if user is None:
        return HTTP_401_UNAUTHORIZED
    jwt_token = create_jwt_token(user)
    return {"token": jwt_token}

request:

在此处输入图像描述

在此处输入图像描述

@app_v1.post("/user/photo")
async def update_photo(response: Response, profile_photo: bytes = File(...)):
    response.headers['x-file-size'] = str(len(profile_photo))
    response.set_cookie(key='cookie-api', value="test")
    return {"profile photo size": len(profile_photo)}

request: 在此处输入图像描述

I acomplished to figure out, it was because when FastAPI was installed, it didn't install python-multipart, so with this package missing everything that needs multipart falls

After installing it works fine

Thanks

The problem with the first request is that you should be sending username and password in a form-data . Instead of x-www-form-urlencoded , use form-data and you should be fine.

在此处输入图像描述

I can't see the problem with the second one. Can you try using Swagger interface and see if the same happens there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM