Fastapi: [QUESTION]Post UploadFile with extra parameter

Created on 3 Jun 2019  路  2Comments  路  Source: tiangolo/fastapi

Description

I try do post some extra data with a UploadFile, how it's possible ?
FastApi return Error: Unprocessable Entity

Example :


class MetadataInfoAdd(BaseModel):
    """
        Metadata Info Add Model
    """
    document: str

class MetadataInfo(BaseModel):
    """
        Metadata Info Model
    """
    metadata_key: str
    metadata_value: str
    metadata_type: str

@app.post("/metadata/add", tags=["stable"], response_model=MetadataInfoAdd)
async def metadata_add(metadata: List[MetadataInfo], file: UploadFile = File(...)):
    """
        Perform POST metadata add
    """
    return {"document" : "STR"}

Return this
0526

I change my async def (remove file parameter) like this :


@app.post("/metadata/add", tags=["stable"], response_model=MetadataInfoAdd)
async def metadata_add(metadata: List[MetadataInfo]):
    """
        Perform POST metadata add
    """
    return {"document" : "STR"}

It's work
0527

Is it possible to do this ?

question

Most helpful comment

It's a limitation of HTTP itself. To upload files the client sends the body payload encoded with "form data".

Any additional data can only be sent using form data, so, simple fields.

It's documented here in a warning: https://fastapi.tiangolo.com/tutorial/request-forms-and-files/#define-file-and-form-parameters

All 2 comments

It's a limitation of HTTP itself. To upload files the client sends the body payload encoded with "form data".

Any additional data can only be sent using form data, so, simple fields.

It's documented here in a warning: https://fastapi.tiangolo.com/tutorial/request-forms-and-files/#define-file-and-form-parameters

Ok thanks for reply :)

Was this page helpful?
0 / 5 - 0 ratings