Skip to content

Added Feature : JSON Request Parsing & File Upload Support

Choose a tag to compare

@Jones-peter Jones-peter released this 02 Dec 11:02
· 109 commits to main since this release

πŸš€ 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 content
  • save(path) – Save file to disk
  • size – File size in bytes
  • content_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! πŸ’™πŸ‡