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 !
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:
not as parameter, is a python keyword.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:

Additional non asked comments:
200 is already the default of successful response on FastAPI.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
notas 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:
Additional non asked comments:
- You don't need to add the status_code in this case, because
200is 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
EmailStrtype 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?
It's possible: https://fastapi.tiangolo.com/tutorial/request-files/#multiple-file-uploads
@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:
Most helpful comment
@Kludex thank you very much for your help and time!