Added Feature : JSON Request Parsing & File Upload Support
π Release Notes β JSON Request Parsing & File Upload Support
This update introduces two major features to JsWeb, significantly improving its capabilities for modern API and form-based web development.
πΉ 1. JSON Request Body Parsing
JsWeb now supports automatic JSON payload parsing for REST API endpoints.
Key Capabilities
- β Automatic parsing when accessing
request.json - β Header validation (
Content-Type: application/json) - β Graceful error handling (returns
{}on invalid JSON) - β Lazy loading (parsed only when accessed)
- β Fully backward compatible
Example
@app.route("/api/users", methods=["POST"])
def create_user(request):
data = request.json # Automatically parsed JSON body
return json({"message": "User created", "user": data})πΉ 2. File Upload Support
Full multipart/form-data handling is now part of JsWeb, making file uploads simple and powerful.
request.files
Access uploaded files directly:
request.files["file"]- Each file is an UploadedFile instance.
UploadedFile Features
read()β Read file contentsave(path)β Save file to disksizeβ File size in bytescontent_typeβ MIME type
πΉ 3. FileField + Validators (Forms)
JsWeb now includes Form support for file inputs.
Supported Validators
- FileRequired() β Ensures a file was submitted
- FileAllowed([...]) β Restricts allowed extensions
- FileSize(max_size=...) β Maximum size validation
Example
from jsweb import Form, FileField, FileRequired, FileAllowed, FileSize
class UploadForm(Form):
document = FileField('Document', validators=[
FileRequired(),
FileAllowed(['pdf', 'doc', 'docx']),
FileSize(max_size=10 * 1024 * 1024) # 10MB
])
@app.route("/upload", methods=["GET", "POST"])
def upload(request):
form = UploadForm(request.form, request.files)
if request.method == "POST" and form.validate():
file = form.document.data
file.save(f"uploads/{file.filename}")
return html(f"Uploaded: {file.filename}")
return render(request, "upload.html", {"form": form})π Special Thanks
A huge thank-you to @kasimlyee for contributing this PR.
Your work brings essential modern features to JsWeb and greatly enhances its usability for real-world applications.
We appreciate your excellent contribution! ππ