Skip to content

Serialize a json string into a Pydantic model in a multipart Form #2499

@Ran4

Description

@Ran4

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

I'm trying to serialize a json string sent in as a multipart form.

This is a minimally reproducible server:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Spec(BaseModel):
    some_field: str

@app.post("/foo")
async def foo(
        spec: Spec = Form(..., ),
        file: List[UploadFile] = File(...),
        ):
    return "success!"

The server will, when called with this request, as sent from python's requests library:

requests.post(url, data={"spec": spec.json()}, files={"file": (filename, file_handle)})

where spec is an instance of Spec above, return 422 with body

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

Environment

  • OS: Ubuntu 20.04
  • FastAPI Version 0.61.1
  • Python version: 3.8.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions