Multipart Form Data

Avatar of Hemanta SundarayHemanta Sundaray

HttpClientRequest.bodyFormData() sends a FormData object as the request body. The Content-Type is automatically set to multipart/form-data with an appropriate boundary string.

This is the standard way to send form data in modern JavaScript, and it’s what you need when the server expects multipart encoding, especially when files are involved.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function submitForm() {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
// Build a FormData object, just like you would in a browser
const formData = new FormData();
formData.append("title", "Effect HTTP Guide");
formData.append("author", "Effect Team");
formData.append("tags", "typescript");
formData.append("tags", "effect");
const request = HttpClientRequest.post("https://httpbin.org/post").pipe(
HttpClientRequest.bodyFormData(formData),
);
const response = yield* client.execute(request);
const data = yield* response.json;
return data;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(submitForm()).then((data) => {
console.log("Form data received:", data.form);
});

Output:

Terminal
Form data received: {
author: 'Effect Team',
tags: [ 'typescript', 'effect' ],
title: 'Effect HTTP Guide'
}

Notice that formData.append was called twice with the same key "tags". Unlike an object (where duplicate keys would overwrite), FormData preserves all values, and the server receives them as an array. This is the same behavior as having multiple <input> elements with the same name attribute in an HTML form.

You construct the FormData using the standard Web API — new FormData(), .append(), .set(), etc. — and pass it directly to bodyFormData. The client handles the multipart encoding and boundary generation.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.