fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Error getting request body: The `python-multipart` library must be installed to use form parsing.

Open q98765543221 opened this issue 6 years ago • 21 comments

(venv) G:\projects\python\fastprojects>pip list

python-dateutil 2.8.1 python-multipart 0.0.5 rope 0.17.0

@app.post('/token', tags=['safe'])
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
  print('form_data', form_data)
  user_dict = fake_users_db.get(form_data.username)
  if not user_dict:
    raise HTTPException(
      status_code=400,
      detail='Incorrect username or password'
    )
  user = UserInDB(**user_dict)
  hashed_password = fake_hash_password(form_data.password)
  if not hashed_password == user.hashed_password:
    raise HTTPException(
      status_code=400,
      detail='Incorrect username or password'
    )
  return {'access_token': user.username, 'token_type': 'bearer'}

I don't know how to troubleshoot this problem

q98765543221 avatar Jun 11 '20 14:06 q98765543221

@q98765543221 pip install python-multipart

Kludex avatar Jun 11 '20 14:06 Kludex

Using legacy setup.py install for python-multipart, since package 'wheel' is not installed. Installing collected packages: python-multipart Running setup.py install for python-multipart ... done Successfully installed python-multipart-0.0.5

?[31mERROR?[0m: Error getting request body: The python-multipart library must be installed to use form parsing . ?[32mINFO?[0m: 127.0.0.1:13645 - "?[1mPOST /token HTTP/1.1?[0m" ?[31m400 Bad Request?[0m

A new project, same error

q98765543221 avatar Jun 11 '20 15:06 q98765543221

Are you sure you're running your app in the same environment as the one you're installing python-multipart?

Kludex avatar Jun 11 '20 16:06 Kludex

(venv) G:\projects\python\fastapi2>uvicorn main:app --reload --port 8090 ?[32mINFO?[0m: Uvicorn running on ?[1mhttp://127.0.0.1:8090?[0m (Press CTRL+C to quit) ?[32mINFO?[0m: Started reloader process [?[36m?[1m10000?[0m] using ?[36m?[1mstatreload?[0m ?[32mINFO?[0m: Started server process [?[36m14376?[0m] ?[32mINFO?[0m: Waiting for application startup. ?[32mINFO?[0m: Application startup complete. ?[32mINFO?[0m: 127.0.0.1:12865 - "?[1mGET /docs HTTP/1.1?[0m" ?[32m200 OK?[0m ?[32mINFO?[0m: 127.0.0.1:12865 - "?[1mGET /openapi.json HTTP/1.1?[0m" ?[32m200 OK?[0m ?[31mERROR?[0m: Error getting request body: The python-multipart library must be installed to use form parsing. ?[32mINFO?[0m: 127.0.0.1:12873 - "?[1mPOST /token HTTP/1.1?[0m" ?[31m400 Bad Request?[0m ?[31mERROR?[0m: Could not stop child process 14376: [Errno 10] No child processes ?[32mINFO?[0m: Stopping reloader process [?[36m?[1m10000?[0m]

(venv) G:\projects\python\fastapi2>pip list Package Version


fastapi 0.55.1 pip 20.1.1 pydantic 1.5.1 python-multipart 0.0.5 setuptools 47.1.1 six 1.15.0 starlette 0.13.2 wheel 0.34.2

q98765543221 avatar Jun 12 '20 04:06 q98765543221

This problem occurs when I use a virtual environment and does not occur when I use a system environment, where the project was created using pyCharm

q98765543221 avatar Jun 12 '20 04:06 q98765543221

Thanks for the help here @Kludex ! :clap: :bow:

I understand you solved your problem, so thanks for reporting back and closing the issue @q98765543221 :+1:

tiangolo avatar Nov 11 '20 18:11 tiangolo

Hitting this exact same problem. It happens with the virtualenv

zackees avatar Jul 22 '22 02:07 zackees

I figured it out. The error being generated right here:

https://github.com/tiangolo/fastapi/blob/5c576e42be8f8832f0e225beaa72153a256892d7/fastapi/dependencies/utils.py#L106

Prints out that the missing library is python-multipart. But this is wrong. If the error is printed out like the following:

image

It will show that the missing package is multipart, and adding this package resolves the issue.

zackees avatar Jul 22 '22 02:07 zackees

Prints out that the missing library is python-multipart. But this is wrong.

Why is this wrong?

JarroVGIT avatar Jul 22 '22 04:07 JarroVGIT

The error output instructs the user to install python-multipart, instead of multipart.

It's also possible that both need to be installed.

zackees avatar Jul 22 '22 06:07 zackees

I don't understand. You need to install python-multipart as per the documentation. Why is it wrong that the error states that you need to install it? pip install multipart will install a different package than pip install python-multipart, both are imported with import multipart. Maybe that is the confusion? Either way, the error message is correct.

JarroVGIT avatar Jul 23 '22 11:07 JarroVGIT

Do you have python-multipart and multipart NOT installed? The program tells you to install python-multipart

Do you have python-multipart and NOT multipart installed? The program still tells you to install python-multipart

The error that is swallowed up? It tells you that the program is missing multipart.

How is this error message correct? It's telling me invalid correction action. If both python-multipart and multipart are installed with multipart, then perhaps the error message should be changed to install multipart.

zackees avatar Jul 23 '22 18:07 zackees

Do you have python-multipart and NOT multipart installed? The program still tells you to install python-multipart

This is not the behaviour on my machine. I do not have multipart installed in my virtual environment, and I do not get this error (macOS, Python 3.10, FastAPI 0.79). What is your environment (os/python/fastapi)?

JarroVGIT avatar Jul 23 '22 19:07 JarroVGIT

Windows 10, python 3.9, FastAPI unknown

zackees avatar Jul 23 '22 20:07 zackees

Do you have a minimal reproducible example of code that triggers this error for you? And please do check your FastAPI version, if it is not current then the first order of business would be to update FastAPI obviously ;)

JarroVGIT avatar Jul 23 '22 21:07 JarroVGIT

The code is in a private repo that I can't share at this time.

But the reproduction case should be equivalent to the following:

@app.post("/upload")
async def upload(file: UploadFile = File(...)) -> PlainTextResponse:
    """Uploads a file to the server."""
    return "ok"

zackees avatar Jul 23 '22 21:07 zackees

Alight. Unfortunately, I am unable to reproduce this behaviour. At this point my suspicion is either an older version of FastAPI or a misconfiguration with your virtual environment but it's impossible to say for sure.

JarroVGIT avatar Jul 23 '22 21:07 JarroVGIT

@zackees Would it be possible that you create a new public repo with a MWE and a requirements.txt that shows the problem you are facing?

chbndrhnns avatar Jul 23 '22 21:07 chbndrhnns

I stripped down the code to reproduce this bug and I can't reproduce it anymore.

Regardless, swallowing up an exception (which shows that the missing library is multipart) is not a good idea.

zackees avatar Jul 25 '22 03:07 zackees

I would have to disagree with that. The name of a package is independent of how you import it (unfortunately). For multipart support, there are two that are imported by import multipart. The exception will tell you wat the name of the package is by import statement, in this case multipart. However, the documentation is clear on that you should install python-multipart. I think it is warranted to replace the exception with a clearer instruction on what to do.

It even has a 'incorrect' statement for when you actually have multipart installed. So I really don't understand what the issue is now, especially if you cannot reproduce it on your end anymore.

JarroVGIT avatar Jul 25 '22 05:07 JarroVGIT