Urgent.News

What's breaking now, across thousands of outlets.

Tech

Multipart form data isn't as mysterious as it looks

If you've ever uploaded a file from a browser, you've probably encountered multipart/form-data . The name sounds more complicated than it really is. A browser submitting a form might send something conceptually like this: --boundary Content-Disposition: form-data; name="name" Seyi --boundary Content-Disposition: form-data; name="avatar"; filename="photo.jpg" Content-Type: image/jpeg <binary JPEG…

Multipart/form-data may not appear as daunting as its name suggests. When a browser uploads a file via a form, it essentially transmits data that can be broken down into several components, such as text fields, files, or other submitted values. These components are linked together using a boundary, which signals the server where one section ends and the next begins.

Initially, I found it challenging to process multipart requests within a Node application, often requiring the use of large and complex abstractions. Libraries such as busboy, formidable, and multer have been employed successfully for years, tackling more extensive issues. However, for an application that operates directly with Node's HTTP streams, I developed node-multipart.

This lightweight solution functions seamlessly with Node's IncomingMessage, presenting the multipart stream as a series of parts. With this approach, the parser manages the technical aspects: identifying multipart boundaries, parsing part headers, handling boundaries that may span network chunks, preserving binary data, streaming part contents, respecting writable-stream backpressure, and adhering to size limitations for per-part and header data.

Meanwhile, the application takes care of the specific requirements, such as determining which fields are permissible, where files should be stored, how filenames should be generated, performing file validation, enforcing authorization, and setting overall request limits. Notably, node-multipart boasts no runtime dependencies, does not generate temporary files, and does not necessitate buffering the entire upload.

The objective was not to supplant the extensive multipart ecosystem but rather to unveil the fundamental operation and provide a compact Node-native primitive for applications that do not require a more comprehensive upload framework. As of now, node-multipart is at version 1.1.2, available on GitHub as mksunny1/node-multipart and on npm as node-multipart.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Django moves to an annual release cycle

The Django Python web-framework project has announced that it has accepted an annual release cycle proposal . This means that the project is moving from a somewhat complicated schedule that…

More from Monday 10 August →