FastAPI request और response model: input validate करें और public data सीमित करें
FastAPI Python type hints और Pydantic models से बताता है कि request data कहाँ से आएगा और उसका आकार क्या होगा। Path parameters URL से, साधारण types डिफ़ॉल्ट रूप से query से, और Pydantic models request body से आते हैं। लौटाए गए data को validate करने, सार्वजनिक fields छाँटने और उसी contract को OpenAPI docs में शामिल करने के लिए response_model तय करें।
FastAPI Python type hints और Pydantic models से बताता है कि request data कहाँ से आएगा और उसका आकार क्या होगा। Path parameters URL से, साधारण types डिफ़ॉल्ट रूप से query से, और Pydantic models request body से आते हैं। लौटाए गए data को validate करने, सार्वजनिक fields छाँटने और उसी contract को OpenAPI docs में शामिल करने के लिए response_model तय करें।
Path operation और parameter के स्रोत समझें
FastAPI app ऐसे decorators दर्ज करता है जो HTTP method और path को function से जोड़ते हैं, जैसे @app.get('/items/{item_id}'). FastAPI function parameters और उनके types पढ़ता है। Path में मौजूद नाम वाला parameter path से मिलता है; int या str जैसे singular types डिफ़ॉल्ट रूप से query parameters होते हैं; Pydantic BaseModel request body होता है।
केवल optional type लिखना पर्याप्त नहीं। q: str | None = None default value के कारण छोड़ा जा सकता है। Type hints editor और static tools को गलतियाँ ढूँढने में मदद करते हैं। Request body आम तौर पर POST, PUT या PATCH में भेजें। GET body का व्यवहार specifications में तय नहीं है और मध्यवर्ती systems उसे न मानें; उससे बचें।
Pydantic से input contract घोषित करें
Body model को BaseModel subclass बनाकर परिभाषित करें और fields के लिए Python types, defaults या Field constraints दें। Field(gt=0) कीमत को शून्य से अधिक रखता है; max_length description सीमित कर सकता है। इससे स्वीकार्य input और validation API docs से जुड़ते हैं।
Update operation item_id को path से, Item model को body से और वैकल्पिक q को query से ले सकती है। FastAPI सही जगह से values पढ़ता है, इसलिए raw JSON को बार-बार हाथ से निकालने और बदलने की जरूरत घटती है। Model validation सभी business rules नहीं है: app को record का होना और caller की उसे बदलने की अनुमति भी जाँचनी होगी।
response_model से output सीमित करें
Response model server के output contract को तय करता है। Decorator में response_model देने पर FastAPI लौटे data को validate और convert करता है, OpenAPI और auto docs में schema जोड़ता है, और model से बाहर के fields छाँटता है। मेल न खाने वाला response server की समस्या है और सामान्यतः response validation error बनता है।
Input और output models अक्सर अलग होने चाहिए। Registration में password चाहिए हो सकता है, लेकिन public user response में नहीं। UserCreate और UserPublic बनाएँ और UserPublic को response_model रखें। इससे public boundary स्पष्ट होती है, handler पर secret न लौटाने की आशा पर निर्भर नहीं रहती।
Return annotation और response_model दोनों हों तो FastAPI validation, docs और filtering के लिए response_model को प्राथमिकता देता है। Function database object लौटाए लेकिन बाहर का रूप Pydantic model होना चाहिए तो यह उपयोगी है। response_model_include/exclude छोटा रास्ता है, मगर OpenAPI पूरा model दिखा सकता है। स्थिर public contract के लिए अलग output model बेहतर है।
Dependencies से जाँच साझा करें
Current user या database session ढूँढने जैसे दोहराए गए काम dependency के रूप में घोषित करें। FastAPI path operation से पहले उन्हें चलाकर परिणाम inject करता है और sub-dependencies को hierarchy में हल करता है। Annotated type जानकारी editor और static checking के लिए बनाए रख सकता है। Sync और async dependencies को sync या async operations के साथ मिलाया जा सकता है।
Dependency के parameters और validation requirements OpenAPI और interactive docs में भी आते हैं। Authentication या authorization dependency जोड़ना नीति सही होने का प्रमाण नहीं है: तय करें किसे अनुमति या अस्वीकृति मिले और protected paths की जाँच करें।
छोटे endpoint में contract जाँचें
UserCreate में username और password, UserPublic में केवल username रखें। Endpoint UserCreate ले और response_model=UserPublic तय करे। अंदरूनी object में अधिक fields हों तब भी public response output model तक सीमित रहेगा। उदाहरण का save_user placeholder है; database write चलने का दावा नहीं।
Development में सत्यापन करें
Python, FastAPI और Pydantic versions देखें और स्थापित versions के अनुरूप APIs लें।
Development server चलाकर operation बुलाएँ। गलत types, missing required fields और constraints उल्लंघन वाले inputs अपेक्षित रूप से reject हों।
सही input भेजकर status और JSON देखें; response model से बाहर के internal fields हटने चाहिए।
/docs या OpenAPI schema में path, query, body और response model जाँचें।
Dependency authorization या shared state देती हो तो allowed और denied दोनों requests जाँचें।
समस्या पर field types/defaults, path names, response_model और dependency scope क्रम से जाँचें। Sensitive field दिखे तो exclusion shortcut पर निर्भर रहने के बजाय public model से हटाकर फिर जाँचें।
FastAPI models केवल input validation और docs नहीं हैं। Response model service से बाहर जाने वाले data की सीमा तय करता है; अलग input और output models आंतरिक data बदलने पर public contract नियंत्रित रखते हैं।
स्रोत
[FastAPI: First Steps](https://fastapi.tiangolo.com/tutorial/first-steps/)
[FastAPI: Request Body](https://fastapi.tiangolo.com/tutorial/body/)
[FastAPI: Response Model](https://fastapi.tiangolo.com/tutorial/response-model/)
[FastAPI: Dependencies](https://fastapi.tiangolo.com/tutorial/dependencies/)
[FastAPI: Body - Fields](https://fastapi.tiangolo.com/tutorial/body-fields/)