File and Form Uploads

Parse multipart and form submissions with streaming storage and limits.

Chapters

  1. Basic multipart/form-data upload
  2. Multiple files (same field name)
  3. application/x-www-form-urlencoded forms
  4. multipart/mixed ordered parts
  5. Nested multipart/mixed
  6. Request-level compression
  7. Part-level compression
  8. OpenAPI: basic multipart upload
  9. OpenAPI: multiple files
  10. OpenAPI: urlencoded forms
  11. OpenAPI: multipart/mixed
  12. OpenAPI: nested multipart
  13. OpenAPI: request-level compression
  14. OpenAPI: part-level compression
  15. OpenAPI: file hash upload
  16. Form rules: file hash upload

Gotchas

  • Add-KrFormRoute injects $FormPayload directly into the runspace (you already have $Context).
  • Content types: Add-KrFormRoute accepts multipart/form-data by default. The urlencoded and multipart/mixed chapters explicitly opt in via KrFormOptions.AllowedRequestContentTypes.
  • For request-level compression examples, make sure you send a real byte[] body with Content-Encoding: gzip (avoid returning an enumerated Object[] of bytes from helper functions).

Multipart rules (KrPartRule)

These examples configure upload validation rules via KrFormOptions.Rules.

  • Rules match by part name (the name="..." value from the Content-Disposition header).
  • For multipart/form-data, this is usually present by default.
  • For ordered multipart/mixed, you must include a Content-Disposition header with a name= for each part if you want rules to apply.
  • If a part has no name, it can still be parsed (as ordered content), but no named rule can match it.

Common rule knobs used in this chapter:

  • Required: Rejects the request if a named part is missing.
  • AllowMultiple: Rejects multiple occurrences when set to $false.
  • AllowedContentTypes / AllowedExtensions: Rejects mismatched uploads.
  • MaxBytes: Rejects oversized parts.

Prerequisites

  • PowerShell 7.4+
  • .NET runtime per repo global.json