[英]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我正在学习使用 FastAPI,在实现一个简单的 API 时,我一遍又一遍地遇到这个错误,我无法弄清楚为什么
"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)}
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我终于弄明白了,这是因为在安装 FastAPI 时,它没有安装 python-multipart,所以这个 package 缺少所有需要 multipart 的东西
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
.第一个请求的问题是您应该在
form-data
中发送username
和password
。 Instead of x-www-form-urlencoded
, use form-data
and you should be fine.而不是
x-www-form-urlencoded
,使用form-data
,你应该没问题。
I can't see the problem with the second one.我看不出第二个有问题。 Can you try using Swagger interface and see if the same happens there?
您可以尝试使用 Swagger 接口,看看那里是否发生同样的情况?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.