Fastapi: Sending parameters with attachments - 422 (Unprocessable Entity)

Created on 30 Jul 2020  路  5Comments  路  Source: tiangolo/fastapi

hello everyone is my first question and I hope to abide by the rules.
I am trying to send my API 4 string type parameters and an array type parameter for attachments, I have tried different ways but I am going to post my last try.

How can I capture my attachments and validate the format type?

class Send(BaseModel):
to: str = Query(..., regex="^([a-zA-Z0-9 -.]+)@([a-zA-Z0-9 -.]+).([a-zA-Z]{2,5})$")
name: str
last_name: str
phone: str
attachments: List[str] = None
#file: UploadFile = File(...) => commented code line.

@app_v1.post("/notify", status_code=HTTP_200_OK)
async def sendNotify(not: Send):

name = not.name
body = "SomeI"
sender_email = "[email protected]"
receiver_email = not.to

message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = name
message["Bcc"] = receiver_email

message.attach(MIMEText(body, "plain"))

#filename = []
filename = not.attachments

with open(filename, "rb") as attachment:
    ....
    ...

thank you !

question

Most helpful comment

@Kludex thank you very much for your help and time!

All 5 comments

hello everyone is my first question and I hope to abide by the rules.

You are not. There's a template when you create the question, which we should follow. I picked the last good issue that I found: https://github.com/tiangolo/fastapi/issues/1802 (as an example).
But thank you for saying it, I feel that you tried. Btw, try to format the code, people usually have more "will" to answer and the question is readable.

Answering the question:

  • You can't use not as parameter, is a python keyword.
  • You can't have an UploadFile inside a pydantic model and expect that you can receive it in the body/query. In your case, what you may want is to have something like this:
from typing import List
from fastapi import FastAPI, Depends, UploadFile, File
from pydantic import BaseModel, Field, EmailStr

app = FastAPI()

class FileQuery(BaseModel):
    to: EmailStr
    name: str
    last_name: str
    phone: str
    attachments: List[str] = None

@app.post("/notify")
async def sendNotify(query: FileQuery = Depends(), file_in: UploadFile = File(...)):
    print(query)

Image showing that it works:
image

Additional non asked comments:

  • You don't need to add the status_code in this case, because 200 is already the default of successful response on FastAPI.
  • Don't use regex for input values, it can be a security issue for ur application, and we have the EmailStr type from Pydantic.

hello everyone is my first question and I hope to abide by the rules.

You are not. There's a template when you create the question, which we should follow. I picked the last good issue that I found: #1802 (as an example).
But thank you for saying it, I feel that you tried. Btw, try to format the code, people usually have more "will" to answer and the question is readable.

Answering the question:

  • You can't use not as parameter, is a python keyword.
  • You can't have an UploadFile inside a pydantic model and expect that you can receive it in the body/query. In your case, what you may want is to have something like this:
from typing import List
from fastapi import FastAPI, Depends, UploadFile, File
from pydantic import BaseModel, Field, EmailStr

app = FastAPI()

class FileQuery(BaseModel):
    to: EmailStr
    name: str
    last_name: str
    phone: str
    attachments: List[str] = None

@app.post("/notify")
async def sendNotify(query: FileQuery = Depends(), file_in: UploadFile = File(...)):
    print(query)

Image showing that it works:
image

Additional non asked comments:

  • You don't need to add the status_code in this case, because 200 is already the default of successful response on FastAPI.
  • Don't use regex for input values, it can be a security issue for ur application, and we have the EmailStr type from Pydantic.

@Kludex
I am very grateful, thank you very much and I will take your advice for the next publications.

I only have one more question: FileUpload allows uploading more than one file? I am looking in the documentation and saw a comment from the year 2019 where it says that FastApi does not support multiple files. Is this really possible?

@Kludex thank you very much for your help and time!

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

Thanks for reporting back and closing the issue @adevits :+1:

Was this page helpful?
0 / 5 - 0 ratings