Put request with file and json leads to "Stream consumed" error

29 Views Asked by At

I'm writing FastAPI endpoint with file and json as arguments. Block below demonstates a part of a fastapi endpoint:

@app.put(f"{settings.API_PREFIX}" + "/project")
async def set_project(request: Request, file: Optional[UploadFile] = File(None)):
    authorization: str = request.headers.get('Authorization')
    content_type: str = request.headers.get('Content-Type')
    request_json = await request.json()

I'm trying to make a request with the following code:

def test_put_project(self):
    files = [('file', open(r'filepath', 'rb'))]
    json = {'uuid': "00000000-0000-0000-0000-000000000000",
            'name': "",
            'meta': "",
            'type': '',
            'path': '',
            'flags': 1,
            }
    resp = requests.put(url=self.url,
                        headers={"Authorization": TOKEN},
                        files=files,
                        json=json
                        )

at the line request_json = await request.json() the endpoint fails with the following error: venv\\Lib\\site-packages\\starlette\\requests.py\", line 218, in stream\n raise RuntimeError(\"Stream consumed\")\nRuntimeError: Stream consumed\n"

I'm expecting that my enpoint correctly handles both: json and file data.

0

There are 0 best solutions below