Request Models 🔥
Provides working with request models
Available JSON, XML, form-data and x-www-form-urlencoded.
Example
model Message: text: string authorId: int serve "127.0.0.1", 5000: post "/[msg:Message]": # by default uses JSON mode return {"response": { "text": msg.text, "author": msg.authorId }}
Modes ðŸ›
Request models parses only JSON raw data by default. To use form-data or x-www-form-urlencoded you should enable it in path params
JSON ✨
This mode used by default
model MyModel: x: int serve "127.0.0.1", 5000: post "/[m:MyModel:json]": return {"response": m.x}
XML ✨
model MyModel: x: int serve "127.0.0.1", 5000: post "/[m:MyModel:xml]": # Body is # <MyModel> # <x type="int">1000</x> # </MyModel> return {"response": m.x}
Form-Data ✨
model UploadImg: img: FormDataItem # this field will parse all data from form-data additionalContext: string = "" # optional string field serve "127.0.0.1", 5000: # Use UploadImg model as form-data model post "/upload/[data:UploadImg:formData]": # working with UploadImg model echo data.img.filename echo data.additionalContext return "Hello"
X-WWW-Form-Urlencoded ✨
model Query: author: int additionalContext: string = "" # optional string field serve "127.0.0.1", 5000: # Use Query model as x-www-form-urlencoded model post "/upload/[data:Query:urlencoded]": # working with Query model echo data.author echo data.additionalContext return "Hello"
Consts
modelFields = r"HappyXModelFields"
modelFieldsGenerics = r"HappyXModelFieldsGenerics"
Macros
macro model(modelName, body: untyped): untyped
-
Creates a new request body model
Allow:
Example:
Simple user model. Supports all models (JSON, XML, Form-Data and x-www-form-urlencoded).
model User: id: int username: string
Simple user model. Supports JSON and XML.
model User{JSON, XML}: id: int username: string
Simple user model with generics. Supports all models (JSON, XML, Form-Data and x-www-form-urlencoded).
model User[T]: id: T username: string
Simple user model with generics. Supports JSON and XML.
model User{JSON, XML}[T]: id: T username: string