Trending

World technology news

→
RFC 7578: Preserving repeated form fields and file bytes
Software Engineering · 2015
Software Engineering2015

RFC 7578: Preserving repeated form fields and file bytes

A station report sends two measurements under one name, an empty comment and a CSV file in one body. A local Node.js example checks the values and file bytes after parsing.

Archive retrospective · Topic year 2015

Developer's JournalPublished: Dec 6, 20266 min read
Reading progress0%
In this article
  1. Two measurements and one file in a single report
  2. How RFC 7578 describes individual parts
  3. Serialize and parse the same request in Node.js
  4. Where ordering affects application behavior
  5. File metadata and content serve distinct purposes
  6. Use the historical standard to frame the current exercise

Two measurements and one file in a single report

A field operator reads values of 12 and 15 from station A-17 and includes a CSV export in the same submission. Both measurements use a field named reading. The client sends an empty comment and does not include an operator field. A server that immediately converts the parsed fields into a dictionary can discard one of the readings or blur the distinction between an empty and absent value. The contract for this report ties it to a station identifier and keeps the order of the measurements until the next processing stage. The empty comment shows that the form supplied the field with no contents. The missing operator field was never sent. Even if a later database layer chooses the same default for both cases, it can make that decision with the original information intact.

How RFC 7578 describes individual parts

RFC 7578 describes multipart/form-data and replaced RFC 2388 in July 2015. A top-level Content-Type header advertises a boundary that separates the parts of the body. Each part can carry headers and data of its own. The form-data disposition gives the field its name. A file part can also carry a filename and its own media type, which is text/csv for this fixture. Repeated parts with the same name retain their order and are not combined. That property gives the receiving application both readings in the order that the sender supplied them. The boundary is a detail of the generated body. A receiver can obtain it from the request header without assuming that the next submission will carry the same value.

Database access flowConceptual sketch · summary of the article's technical flow
Illustrative diagram. Two repeated fields and one file travel in a single body.

Serialize and parse the same request in Node.js

The local example appends the two readings separately. It builds a three-line CSV with CRLF line endings and supplies a media type in the file part. When Request receives a FormData body, it generates a matching Content-Type header. The test reads a cloned request to confirm that the serialized body has two separate reading parts. It then parses the original request and fetches both readings through getAll. A separate check distinguishes the empty comment from the missing operator. Finally it verifies the file name, media type and every byte of the fixture. Node.js 24.19.0 produced the readings 12 and 15, an empty string for the comment, null for the operator, and a 26-byte file. This test reads an in-process request and sends no network traffic.

import assert from 'node:assert/strict';

const csv = 'sample,value\r\n1,12\r\n2,15\r\n';
const form = new FormData();
form.append('station', 'A-17');
form.append('reading', '12');
form.append('reading', '15');
form.append('comment', '');
form.append('export', new Blob([csv], {type:'text/csv'}), 'sample.csv');

const request = new Request('http://localhost/upload', {method:'POST',body:form});
const type = request.headers.get('content-type');
assert.match(type, /^multipart\/form-data;\s*boundary=/i);
const wire = await request.clone().text();
assert.equal([...wire.matchAll(/name="reading"/g)].length, 2);
const parsed = await request.formData();
assert.equal(parsed.get('station'), 'A-17');
assert.deepEqual(parsed.getAll('reading'), ['12','15']);
assert.equal(parsed.get('comment'), '');
assert.equal(parsed.get('operator'), null);
const file = parsed.get('export');
assert.equal(file.name, 'sample.csv');
assert.equal(file.type, 'text/csv');
assert.deepEqual(Buffer.from(await file.arrayBuffer()), Buffer.from(csv));
console.log(JSON.stringify({readings:parsed.getAll('reading'),empty:parsed.get('comment'),missing:parsed.get('operator'),bytes:file.size}));

Where ordering affects application behavior

A plain average of two numbers gives the same answer regardless of order. A system that records the first and last reading has a different requirement. A conversion that appears harmless for one reporting operation may silently change another. In the FormData interface, get returns a single value and getAll obtains every value stored under that name. A receiving endpoint can define a rule for duplicate values when an application field is meant to be singular. The test in this example expects both readings and preserves their order. Repeated file uploads can also place separate files in separate parts under the same name. The service owns the limits on the number and size of those parts, even when the interface used to submit the form already has client-side validation.

File metadata and content serve distinct purposes

A received filename can be displayed in a record after the application handles it appropriately. It should not become the storage path directly. An application can assign its own storage identifier and retain a supplied name as separate metadata. The declared part type helps tell a receiver how the sender described the file. The application still checks the contents it expects to process. Here, comparing all 26 bytes ensures that no logic altered the line endings while parsing a text file. A production CSV reader would also apply limits on size and rows and validate the expected record structure. When support investigates an incorrect file, the declared media type, stored display name, and result of content validation answer separate questions about what the receiver saw.

Use the historical standard to frame the current exercise

The 2015 document defines the wire format. This particular exercise runs on a modern version of Node.js and offers no measurement of older browser or server implementations. An integration test for an actual endpoint also needs to exercise the network transfer, proxy size limits, temporary-file cleanup and the parser's streaming behavior. The controlled fixture gives the integration a smaller starting point. Two readings must remain ordered, the empty and missing fields must be distinguishable, and the file must preserve its bytes. When an application changes libraries, those checks keep the same expectations for the data. A larger test suite can let each supported client choose its own boundary while checking that the values and file contents delivered to the application remain the same.

345 reads ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.