
A simple multipart parser 👑 Written in Nim language
nimble install multipart
- Parses multipart/form-data content from HTTP requests
- Supports file uploads and text fields
- Progress callbacks for monitoring parsing progress (body start/done, file start/chunk/done)
- Configurable size limits for files and overall body
- Callbacks for handling file data during parsing (magic number validation, custom processing)
- Automatic cleanup of temporary files after processing
import multipart
let inputData = "..." # Your raw multipart/form-data body as a string or byte array
let contentType = "multipart/form-data; boundary=----WebKitFormBoundaryABC123"
# content type header from the HTTP request, including the boundary
# Progress callback to track parsing progress
proc progressCallback(evt: MultipartProgress) =
case evt.kind
of progressBodyStart:
echo "Parsing started. Total bytes: ", evt.totalBytes
of progressFileStart:
echo "File upload started: ", evt.fileName
of progressFileChunk:
echo "Processing chunk... Bytes written: ", evt.bytesWritten
of progressFileDone:
echo "File upload completed: ", evt.fileName
of progressBodyDone:
echo "Parsing completed. Total bytes: ", evt.totalBytes
# Initialize the multipart parser
var mp = initMultipart(contentType, tmpDir = getTempDir() / "multipart_example")
mp.progressCallback = progressCallback
# optionally, add a progress callback to monitor parsing progress (body start/done, file start/chunk/done)
# Parse the multipart data
mp.parse(inputData)
# Display parsed data
for b in mp:
case b.dataType
of MultipartText:
echo "Text field: ", b.fieldName, " = ", b.value
of MultipartFile:
echo "File field: ", b.fieldName, ", File name: ", b.fileName
if fileExists(b.getPath):
echo "Stored file path: ", b.getPathIf you're looking for a full featured input validator you can use openpeeps/bag package to validate input data, forms, including multipart/form-data. Give a try https://github.com/openpeeps/bag
- 🐛 Found a bug? Create a new Issue
- 👋 Wanna help? Fork it!
- 😎 Get €20 in cloud credits from Hetzner
MIT license. Made by Humans from OpenPeeps.
Copyright © 2024 OpenPeeps & Contributors — All rights reserved.