Sending Raw Binary Data

Avatar of Hemanta SundarayHemanta Sundaray

The simplest way to send binary data is HttpClientRequest.bodyUint8Array(). It sets the request body to a raw byte array and optionally sets the Content-Type header.

This is useful when you already have the file contents in memory as a Uint8Array, for example, after reading a file, generating data programmatically, or receiving bytes from another source.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function sendBinaryData() {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
// Create some binary data (in practice, this might come from a file)
const encoder = new TextEncoder();
const bytes = encoder.encode("Hello from Effect!");
const request = HttpClientRequest.post("https://httpbin.org/post").pipe(
HttpClientRequest.bodyUint8Array(bytes, "text/plain"),
);
const response = yield* client.execute(request);
const data = yield* response.json;
return data;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(sendBinaryData()).then((data) => {
console.log("Content-Type sent:", data.headers["Content-Type"]);
console.log("Body received:", data.data);
});

Output:

Terminal
Content-Type sent: text/plain
Body received: Hello from Effect!

bodyUint8Array takes two arguments: the byte array and an optional content type string. If you omit the content type, it defaults to application/octet-stream (generic binary).

This API loads the entire payload into memory, so it’s best for small-to-medium payloads. For large files, use streaming instead.

Sign in to save progress

Stay in the loop

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