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

Rodrigo A
  • 657
  • 7
  • 23

2 Answers2

17

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

Rodrigo A
  • 657
  • 7
  • 23
  • 1
    After installing `python-multipart` just like what you did, my form data worked, thank you! – Akio Sep 30 '20 at 06:36
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?

Marcelo Trylesinski
  • 634
  • 1
  • 5
  • 13
  • I've tried form data too, but still gets the same error, even from the swagger. I edit question to show the swagger print – Rodrigo A Jun 17 '20 at 01:46