4

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:

enter image description here

enter image description here

@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: enter image description here

1

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

1 Comment

After installing python-multipart just like what you did, my form data worked, thank you!
0

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.

enter image description here

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

1 Comment

I've tried form data too, but still gets the same error, even from the swagger. I edit question to show the swagger print

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.