1

I want to post a file and a json object to a route in my FastAPI in only one request, but doing so results in errors like:

422
{"detail":[{"loc":["body"],"msg":"value is not a valid list","type":"type_error.list"},{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}

This is the client file:

import requests

url = "http://localhost:8000/"
file = "..."
data = {...}

files = {"pdf": (file, open(file, "rb"), "application/pdf")}

response = requests.post(url, json=data, files=files)

I have also tried setting the headers to multipart/form-data and multipart/reated

This is what my server file looks like:

...

class SomeMetadata(BaseModel): ...
...
@app.post("/")
def submit(someMetadata: SomeMetadata = Body(...), pdf: UploadFile = File(...)):
        return ...
Anm
  • 447
  • 4
  • 15
  • afaik, you can not do this as you are [trying to mix up](https://stackoverflow.com/questions/5809099/does-the-http-protocol-support-multiple-content-types-in-response-headers) the `application/json` and `multipart/form-data` content types in a single request – JPG Apr 16 '23 at 15:00
  • 1
    For any future visitors I would like to add that [this answer](https://stackoverflow.com/a/71891621/21502948) was the one that worked for me – Anm Apr 17 '23 at 12:41

0 Answers0